xref: /aosp_15_r20/system/sepolicy/private/crosvm.te (revision e4a36f4174b17bbab9dc043f4a65dc8d87377290)
1*e4a36f41SAndroid Build Coastguard Workertype crosvm, domain, coredomain;
2*e4a36f41SAndroid Build Coastguard Workertype crosvm_exec, system_file_type, exec_type, file_type;
3*e4a36f41SAndroid Build Coastguard Workertype crosvm_tmpfs, file_type;
4*e4a36f41SAndroid Build Coastguard Worker
5*e4a36f41SAndroid Build Coastguard Worker# Let crosvm open VM manager devices such as /dev/kvm.
6*e4a36f41SAndroid Build Coastguard Workerallow crosvm vm_manager_device_type:chr_file rw_file_perms;
7*e4a36f41SAndroid Build Coastguard Worker
8*e4a36f41SAndroid Build Coastguard Worker# Most other domains shouldn't access /dev/kvm.
9*e4a36f41SAndroid Build Coastguard Workerneverallow { domain -crosvm -ueventd -shell } kvm_device:chr_file getattr;
10*e4a36f41SAndroid Build Coastguard Workerneverallow { domain -crosvm -ueventd } kvm_device:chr_file ~getattr;
11*e4a36f41SAndroid Build Coastguard Workerneverallowxperm { domain -crosvm } kvm_device:chr_file ioctl ~{ KVM_CHECK_EXTENSION };
12*e4a36f41SAndroid Build Coastguard Worker
13*e4a36f41SAndroid Build Coastguard Worker# Most other domains shouldn't access other vm managers either.
14*e4a36f41SAndroid Build Coastguard Worker# These restrictions need to be slightly looser than for kvm_device to allow
15*e4a36f41SAndroid Build Coastguard Worker# for different implementations.
16*e4a36f41SAndroid Build Coastguard Workerneverallow { coredomain appdomain -crosvm -ueventd -shell } vm_manager_device_type:chr_file getattr;
17*e4a36f41SAndroid Build Coastguard Workerneverallow { coredomain appdomain -crosvm -ueventd } vm_manager_device_type:chr_file ~getattr;
18*e4a36f41SAndroid Build Coastguard Worker
19*e4a36f41SAndroid Build Coastguard Worker# Let crosvm create temporary files.
20*e4a36f41SAndroid Build Coastguard Workertmpfs_domain(crosvm)
21*e4a36f41SAndroid Build Coastguard Worker
22*e4a36f41SAndroid Build Coastguard Worker# Let crosvm receive file descriptors from VirtualizationService.
23*e4a36f41SAndroid Build Coastguard Workerallow crosvm {
24*e4a36f41SAndroid Build Coastguard Worker  virtualizationmanager
25*e4a36f41SAndroid Build Coastguard Worker  is_flag_enabled(RELEASE_AVF_ENABLE_EARLY_VM, `early_virtmgr')
26*e4a36f41SAndroid Build Coastguard Worker}:fd use;
27*e4a36f41SAndroid Build Coastguard Worker
28*e4a36f41SAndroid Build Coastguard Worker# Allow sending VirtualizationService the failure reason and console/log from the VM via pipe.
29*e4a36f41SAndroid Build Coastguard Workerallow crosvm {
30*e4a36f41SAndroid Build Coastguard Worker  virtualizationmanager
31*e4a36f41SAndroid Build Coastguard Worker  is_flag_enabled(RELEASE_AVF_ENABLE_EARLY_VM, `early_virtmgr')
32*e4a36f41SAndroid Build Coastguard Worker}:fifo_file write;
33*e4a36f41SAndroid Build Coastguard Worker
34*e4a36f41SAndroid Build Coastguard Worker# Let crosvm read the composite disk images (virtualizationservice_data_file), APEXes
35*e4a36f41SAndroid Build Coastguard Worker# (staging_data_file), APKs (apk_data_file and shell_data_file where the latter is for test apks in
36*e4a36f41SAndroid Build Coastguard Worker# /data/local/tmp), instance.img (app_data_file), and microdroid vendor image (vendor_microdroid_file).
37*e4a36f41SAndroid Build Coastguard Worker# Allow crosvm to read the instance image of the service VM saved in apex_virt_data_file.
38*e4a36f41SAndroid Build Coastguard Worker# Note that the open permission is not given as the files are passed as file descriptors.
39*e4a36f41SAndroid Build Coastguard Workerallow crosvm {
40*e4a36f41SAndroid Build Coastguard Worker  virtualizationservice_data_file
41*e4a36f41SAndroid Build Coastguard Worker  staging_data_file
42*e4a36f41SAndroid Build Coastguard Worker  apk_data_file
43*e4a36f41SAndroid Build Coastguard Worker  app_data_file
44*e4a36f41SAndroid Build Coastguard Worker  privapp_data_file
45*e4a36f41SAndroid Build Coastguard Worker  apex_compos_data_file
46*e4a36f41SAndroid Build Coastguard Worker  apex_virt_data_file
47*e4a36f41SAndroid Build Coastguard Worker  shell_data_file
48*e4a36f41SAndroid Build Coastguard Worker  vendor_microdroid_file
49*e4a36f41SAndroid Build Coastguard Worker  is_flag_enabled(RELEASE_AVF_ENABLE_EARLY_VM, `vm_data_file')
50*e4a36f41SAndroid Build Coastguard Worker}:file { getattr read ioctl lock };
51*e4a36f41SAndroid Build Coastguard Worker
52*e4a36f41SAndroid Build Coastguard Worker# Allow searching the directory where the composite disk images are.
53*e4a36f41SAndroid Build Coastguard Workerallow crosvm {
54*e4a36f41SAndroid Build Coastguard Worker    virtualizationservice_data_file
55*e4a36f41SAndroid Build Coastguard Worker    is_flag_enabled(RELEASE_AVF_ENABLE_EARLY_VM, `vm_data_file')
56*e4a36f41SAndroid Build Coastguard Worker}:dir search;
57*e4a36f41SAndroid Build Coastguard Worker
58*e4a36f41SAndroid Build Coastguard Worker# When running a VM as root we get spurious capability denials.
59*e4a36f41SAndroid Build Coastguard Worker# Suppress them.
60*e4a36f41SAndroid Build Coastguard Workeruserdebug_or_eng(`
61*e4a36f41SAndroid Build Coastguard Worker  dontaudit crosvm self:capability ipc_lock;
62*e4a36f41SAndroid Build Coastguard Worker')
63*e4a36f41SAndroid Build Coastguard Worker
64*e4a36f41SAndroid Build Coastguard Worker# Allow crosvm to tune for performance.
65*e4a36f41SAndroid Build Coastguard Workerallow crosvm self:global_capability_class_set sys_nice;
66*e4a36f41SAndroid Build Coastguard Worker
67*e4a36f41SAndroid Build Coastguard Worker# Let crosvm access its control socket as created by VS.
68*e4a36f41SAndroid Build Coastguard Worker#   read, write, getattr: listener socket polling
69*e4a36f41SAndroid Build Coastguard Worker#   accept: listener socket accepting new connection
70*e4a36f41SAndroid Build Coastguard Worker# Note that the open permission is not given as the socket is passed by FD.
71*e4a36f41SAndroid Build Coastguard Workerallow crosvm {
72*e4a36f41SAndroid Build Coastguard Worker  virtualizationmanager
73*e4a36f41SAndroid Build Coastguard Worker  is_flag_enabled(RELEASE_AVF_ENABLE_EARLY_VM, `early_virtmgr')
74*e4a36f41SAndroid Build Coastguard Worker}:unix_stream_socket { accept read write getattr getopt };
75*e4a36f41SAndroid Build Coastguard Worker
76*e4a36f41SAndroid Build Coastguard Worker# Let crosvm open test artifacts under /data/local/tmp with file path. (e.g. custom pvmfw.img)
77*e4a36f41SAndroid Build Coastguard Workeruserdebug_or_eng(`
78*e4a36f41SAndroid Build Coastguard Worker  allow crosvm shell_data_file:dir search;
79*e4a36f41SAndroid Build Coastguard Worker  allow crosvm shell_data_file:file open;
80*e4a36f41SAndroid Build Coastguard Worker')
81*e4a36f41SAndroid Build Coastguard Worker
82*e4a36f41SAndroid Build Coastguard Worker# The instance image and the composite image should be writable as well because they could represent
83*e4a36f41SAndroid Build Coastguard Worker# mutable disks.
84*e4a36f41SAndroid Build Coastguard Workerallow crosvm {
85*e4a36f41SAndroid Build Coastguard Worker  virtualizationservice_data_file
86*e4a36f41SAndroid Build Coastguard Worker  app_data_file
87*e4a36f41SAndroid Build Coastguard Worker  privapp_data_file
88*e4a36f41SAndroid Build Coastguard Worker  apex_compos_data_file
89*e4a36f41SAndroid Build Coastguard Worker  apex_virt_data_file
90*e4a36f41SAndroid Build Coastguard Worker  is_flag_enabled(RELEASE_AVF_ENABLE_EARLY_VM, `vm_data_file')
91*e4a36f41SAndroid Build Coastguard Worker}:file write;
92*e4a36f41SAndroid Build Coastguard Worker
93*e4a36f41SAndroid Build Coastguard Worker# Allow crosvm to pipe console log to shell or app which could be the owner of a VM.
94*e4a36f41SAndroid Build Coastguard Workerallow crosvm adbd:fd use;
95*e4a36f41SAndroid Build Coastguard Workerallow crosvm adbd:unix_stream_socket { read write };
96*e4a36f41SAndroid Build Coastguard Workerallow crosvm devpts:chr_file { read write getattr ioctl };
97*e4a36f41SAndroid Build Coastguard Worker
98*e4a36f41SAndroid Build Coastguard Workeris_flag_enabled(RELEASE_AVF_SUPPORT_CUSTOM_VM_WITH_PARAVIRTUALIZED_DEVICES, `
99*e4a36f41SAndroid Build Coastguard Worker  # Allow crosvm to draw screen in the surface
100*e4a36f41SAndroid Build Coastguard Worker  allow crosvm device:dir { read open };
101*e4a36f41SAndroid Build Coastguard Worker  allow crosvm same_process_hal_file:file { read open getattr map execute };
102*e4a36f41SAndroid Build Coastguard Worker  allow crosvm gpu_device:chr_file { read write open ioctl map open getattr };
103*e4a36f41SAndroid Build Coastguard Worker  allow crosvm hal_graphics_allocator:fd use;
104*e4a36f41SAndroid Build Coastguard Worker  allow crosvm hal_graphics_allocator_server:binder call;
105*e4a36f41SAndroid Build Coastguard Worker  allow crosvm surfaceflinger:fd use;
106*e4a36f41SAndroid Build Coastguard Worker  hal_client_domain(crosvm, hal_graphics_allocator)
107*e4a36f41SAndroid Build Coastguard Worker
108*e4a36f41SAndroid Build Coastguard Worker  # To provide display service to an app to get surface.
109*e4a36f41SAndroid Build Coastguard Worker  # TODO(b/332677707): remove them when display service uses binder RPC.
110*e4a36f41SAndroid Build Coastguard Worker  allow crosvm vmlauncher_app:binder { transfer call };
111*e4a36f41SAndroid Build Coastguard Worker  allow crosvm servicemanager:binder { call transfer };
112*e4a36f41SAndroid Build Coastguard Worker  allow crosvm virtualization_service:service_manager find;
113*e4a36f41SAndroid Build Coastguard Worker  allow crosvm virtualizationservice:binder { call transfer };
114*e4a36f41SAndroid Build Coastguard Worker
115*e4a36f41SAndroid Build Coastguard Worker  # Allow crosvm to play sound.
116*e4a36f41SAndroid Build Coastguard Worker  binder_call(crosvm, audioserver)
117*e4a36f41SAndroid Build Coastguard Worker  allow crosvm audioserver_service:service_manager find;
118*e4a36f41SAndroid Build Coastguard Worker
119*e4a36f41SAndroid Build Coastguard Worker  # Allow crosvm to mount fuse path in guest VM through virtiofs
120*e4a36f41SAndroid Build Coastguard Worker  allow crosvm fuse:file create_file_perms;
121*e4a36f41SAndroid Build Coastguard Worker  allow crosvm fuse:dir create_dir_perms;
122*e4a36f41SAndroid Build Coastguard Worker  allow crosvm mnt_user_file:dir search;
123*e4a36f41SAndroid Build Coastguard Worker
124*e4a36f41SAndroid Build Coastguard Worker  # Allow crosvm to create unix socket for vhost-user-fs
125*e4a36f41SAndroid Build Coastguard Worker  allow crosvm virtualizationservice_data_file:dir { add_name write remove_name };
126*e4a36f41SAndroid Build Coastguard Worker  allow crosvm virtualizationservice_data_file:sock_file { create write unlink };
127*e4a36f41SAndroid Build Coastguard Worker')
128*e4a36f41SAndroid Build Coastguard Worker
129*e4a36f41SAndroid Build Coastguard Worker# crosvm tries to use netlink sockets as part its APCI implementation, but we don't need it for AVF (b/228077254)
130*e4a36f41SAndroid Build Coastguard Workerdontaudit crosvm self:netlink_generic_socket create_socket_perms_no_ioctl;
131*e4a36f41SAndroid Build Coastguard Worker
132*e4a36f41SAndroid Build Coastguard Worker# crosvm can write files in /data/local/tmp which are usually used for instance.img and logging by
133*e4a36f41SAndroid Build Coastguard Worker# compliance tests and demo apps. Write access to instance.img is particularily important because
134*e4a36f41SAndroid Build Coastguard Worker# the VM has to initialize the disk image on its first boot. Note that open access is still not
135*e4a36f41SAndroid Build Coastguard Worker# granted because the files are expected to be opened by the owner of the VM (apps or shell in case
136*e4a36f41SAndroid Build Coastguard Worker# when the vm is created by the `vm` tool) and handed over to crosvm as FD.
137*e4a36f41SAndroid Build Coastguard Workerallow crosvm shell_data_file:file write;
138*e4a36f41SAndroid Build Coastguard Worker
139*e4a36f41SAndroid Build Coastguard Worker# crosvm tries to read serial device, including the write-only pipe from virtualizationmanager (to
140*e4a36f41SAndroid Build Coastguard Worker# forward console/log to the host logcat).
141*e4a36f41SAndroid Build Coastguard Worker# crosvm only needs write permission, so dontaudit read
142*e4a36f41SAndroid Build Coastguard Workerdontaudit crosvm {
143*e4a36f41SAndroid Build Coastguard Worker  virtualizationmanager
144*e4a36f41SAndroid Build Coastguard Worker  is_flag_enabled(RELEASE_AVF_ENABLE_EARLY_VM, `early_virtmgr')
145*e4a36f41SAndroid Build Coastguard Worker}:fifo_file { read getattr };
146*e4a36f41SAndroid Build Coastguard Worker
147*e4a36f41SAndroid Build Coastguard Worker# Required for crosvm to start gdb-server to enable debugging of guest kernel.
148*e4a36f41SAndroid Build Coastguard Workerallow crosvm self:tcp_socket { bind create read setopt write accept listen };
149*e4a36f41SAndroid Build Coastguard Workerallow crosvm port:tcp_socket name_bind;
150*e4a36f41SAndroid Build Coastguard Workerallow crosvm adbd:unix_stream_socket ioctl;
151*e4a36f41SAndroid Build Coastguard Workerallow crosvm node:tcp_socket node_bind;
152*e4a36f41SAndroid Build Coastguard Worker
153*e4a36f41SAndroid Build Coastguard Worker# Allow crosvm to interact to VFIO device
154*e4a36f41SAndroid Build Coastguard Workerallow crosvm vfio_device:chr_file rw_file_perms;
155*e4a36f41SAndroid Build Coastguard Workerallow crosvm vfio_device:dir r_dir_perms;
156*e4a36f41SAndroid Build Coastguard Worker
157*e4a36f41SAndroid Build Coastguard Worker# Allow crosvm to access VM DTBO via a file created by virtualizationmanager.
158*e4a36f41SAndroid Build Coastguard Workerallow crosvm virtualizationservice_data_file:file read;
159*e4a36f41SAndroid Build Coastguard Worker
160*e4a36f41SAndroid Build Coastguard Workeris_flag_enabled(RELEASE_AVF_ENABLE_NETWORK, `
161*e4a36f41SAndroid Build Coastguard Worker    # Allow crosvm to deal with file descriptors of TAP interfaces.
162*e4a36f41SAndroid Build Coastguard Worker    allow crosvm tun_device:chr_file rw_file_perms;
163*e4a36f41SAndroid Build Coastguard Worker    allowxperm crosvm tun_device:chr_file ioctl { TUNGETIFF TUNSETOFFLOAD TUNSETVNETHDRSZ };
164*e4a36f41SAndroid Build Coastguard Worker    allow crosvm self:udp_socket create_socket_perms;
165*e4a36f41SAndroid Build Coastguard Worker    allowxperm crosvm self:udp_socket ioctl SIOCGIFMTU;
166*e4a36f41SAndroid Build Coastguard Worker    allow crosvm vmnic:fd use;
167*e4a36f41SAndroid Build Coastguard Worker')
168*e4a36f41SAndroid Build Coastguard Worker
169*e4a36f41SAndroid Build Coastguard Worker# Early VMs may print messages to kmsg_debug_device.
170*e4a36f41SAndroid Build Coastguard Workerallow crosvm kmsg_debug_device:chr_file w_file_perms;
171*e4a36f41SAndroid Build Coastguard Worker
172*e4a36f41SAndroid Build Coastguard Worker# Don't allow crosvm to open files that it doesn't own.
173*e4a36f41SAndroid Build Coastguard Worker# This is important because a malicious application could try to start a VM with a composite disk
174*e4a36f41SAndroid Build Coastguard Worker# image referring by name to files which it doesn't have permission to open, trying to get crosvm to
175*e4a36f41SAndroid Build Coastguard Worker# open them on its behalf. By preventing crosvm from opening any other files we prevent this
176*e4a36f41SAndroid Build Coastguard Worker# potential privilege escalation. See http://b/192453819 for more discussion.
177*e4a36f41SAndroid Build Coastguard Workerneverallow crosvm {
178*e4a36f41SAndroid Build Coastguard Worker  virtualizationservice_data_file
179*e4a36f41SAndroid Build Coastguard Worker  staging_data_file
180*e4a36f41SAndroid Build Coastguard Worker  apk_data_file
181*e4a36f41SAndroid Build Coastguard Worker  app_data_file
182*e4a36f41SAndroid Build Coastguard Worker  privapp_data_file
183*e4a36f41SAndroid Build Coastguard Worker  is_flag_enabled(RELEASE_UNLOCKED_STORAGE_API, `storage_area_content_file')
184*e4a36f41SAndroid Build Coastguard Worker  is_flag_enabled(RELEASE_AVF_ENABLE_EARLY_VM, `vm_data_file')
185*e4a36f41SAndroid Build Coastguard Worker  userdebug_or_eng(`-shell_data_file')
186*e4a36f41SAndroid Build Coastguard Worker}:file open;
187*e4a36f41SAndroid Build Coastguard Worker
188*e4a36f41SAndroid Build Coastguard Worker# Don't allow crosvm to have access to ordinary vendor files that are not for VMs.
189*e4a36f41SAndroid Build Coastguard Workerfull_treble_only(`
190*e4a36f41SAndroid Build Coastguard Worker  neverallow crosvm {
191*e4a36f41SAndroid Build Coastguard Worker    vendor_file_type
192*e4a36f41SAndroid Build Coastguard Worker    -vendor_vm_file
193*e4a36f41SAndroid Build Coastguard Worker    -vendor_vm_data_file
194*e4a36f41SAndroid Build Coastguard Worker    # These types are not required for crosvm, but the access is granted to globally in domain.te
195*e4a36f41SAndroid Build Coastguard Worker    # thus should be exempted here.
196*e4a36f41SAndroid Build Coastguard Worker    -vendor_cgroup_desc_file
197*e4a36f41SAndroid Build Coastguard Worker    -vendor_configs_file
198*e4a36f41SAndroid Build Coastguard Worker    -vendor_microdroid_file
199*e4a36f41SAndroid Build Coastguard Worker    -vndk_sp_file
200*e4a36f41SAndroid Build Coastguard Worker    -vendor_task_profiles_file
201*e4a36f41SAndroid Build Coastguard Worker    -same_process_hal_file
202*e4a36f41SAndroid Build Coastguard Worker  }:file *;
203*e4a36f41SAndroid Build Coastguard Worker')
204*e4a36f41SAndroid Build Coastguard Worker
205*e4a36f41SAndroid Build Coastguard Worker# Only allow crosvm to read app data files for clients that can start
206*e4a36f41SAndroid Build Coastguard Worker# VMs. Note that the use of app data files is further restricted
207*e4a36f41SAndroid Build Coastguard Worker# inside the virtualizationservice by checking the label of all disk
208*e4a36f41SAndroid Build Coastguard Worker# image files.
209*e4a36f41SAndroid Build Coastguard Workerneverallow crosvm {
210*e4a36f41SAndroid Build Coastguard Worker  app_data_file_type
211*e4a36f41SAndroid Build Coastguard Worker  -app_data_file
212*e4a36f41SAndroid Build Coastguard Worker  -privapp_data_file
213*e4a36f41SAndroid Build Coastguard Worker  -shell_data_file
214*e4a36f41SAndroid Build Coastguard Worker}:file read;
215*e4a36f41SAndroid Build Coastguard Worker
216*e4a36f41SAndroid Build Coastguard Worker# Only virtualizationmanager can run crosvm
217*e4a36f41SAndroid Build Coastguard Worker# Allow vmlauncher app to launch crosvm for virtiofs
218*e4a36f41SAndroid Build Coastguard Workerneverallow {
219*e4a36f41SAndroid Build Coastguard Worker  domain
220*e4a36f41SAndroid Build Coastguard Worker  -crosvm
221*e4a36f41SAndroid Build Coastguard Worker  -virtualizationmanager
222*e4a36f41SAndroid Build Coastguard Worker  -vmlauncher_app
223*e4a36f41SAndroid Build Coastguard Worker
224*e4a36f41SAndroid Build Coastguard Worker  is_flag_enabled(RELEASE_AVF_ENABLE_EARLY_VM, `-early_virtmgr')
225*e4a36f41SAndroid Build Coastguard Worker} crosvm_exec:file no_x_file_perms;
226