blob: 4daa0cffececcafe3bd82ca0ae31daf9397796df [file] [log] [blame]
David Brazdilafc9a9e2023-01-12 16:08:10 +00001// Copyright 2021, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Implementation of the AIDL interface of the VirtualizationService.
16
17use crate::{get_calling_pid, get_calling_uid};
David Brazdil33a31022023-01-12 16:55:16 +000018use crate::atom::{forward_vm_booted_atom, forward_vm_creation_atom, forward_vm_exited_atom};
Alice Wanga410b642023-10-18 09:05:15 +000019use crate::rkpvm::request_attestation;
David Brazdilafc9a9e2023-01-12 16:08:10 +000020use android_os_permissions_aidl::aidl::android::os::IPermissionController;
Alice Wangc2fec932023-02-23 16:24:02 +000021use android_system_virtualizationservice::{
Inseob Kim53d0b212023-07-20 16:58:37 +090022 aidl::android::system::virtualizationservice::AssignableDevice::AssignableDevice,
Alice Wangc2fec932023-02-23 16:24:02 +000023 aidl::android::system::virtualizationservice::VirtualMachineDebugInfo::VirtualMachineDebugInfo,
24 binder::ParcelFileDescriptor,
25};
David Brazdilafc9a9e2023-01-12 16:08:10 +000026use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::{
27 AtomVmBooted::AtomVmBooted,
28 AtomVmCreationRequested::AtomVmCreationRequested,
29 AtomVmExited::AtomVmExited,
30 IGlobalVmContext::{BnGlobalVmContext, IGlobalVmContext},
Inseob Kim7307a892023-09-14 13:37:58 +090031 IVirtualizationServiceInternal::BoundDevice::BoundDevice,
David Brazdilafc9a9e2023-01-12 16:08:10 +000032 IVirtualizationServiceInternal::IVirtualizationServiceInternal,
Inseob Kimbdca0472023-07-28 19:20:56 +090033 IVfioHandler::{BpVfioHandler, IVfioHandler},
David Brazdilafc9a9e2023-01-12 16:08:10 +000034};
35use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::VM_TOMBSTONES_SERVICE_PORT;
Alice Wangd1b11a02023-04-18 12:30:20 +000036use anyhow::{anyhow, ensure, Context, Result};
Jiyong Parkd7bd2f22023-08-10 20:41:19 +090037use avflog::LogResult;
Jiyong Park2227eaa2023-08-04 11:59:18 +090038use binder::{self, wait_for_interface, BinderFeatures, ExceptionCode, Interface, LazyServiceGuard, Status, Strong, IntoBinderResult};
David Brazdilafc9a9e2023-01-12 16:08:10 +000039use libc::VMADDR_CID_HOST;
40use log::{error, info, warn};
41use rustutils::system_properties;
Inseob Kimc4a774d2023-08-30 12:48:43 +090042use serde::Deserialize;
43use std::collections::{HashMap, HashSet};
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +090044use std::fs::{self, create_dir, remove_dir_all, set_permissions, File, Permissions};
David Brazdilafc9a9e2023-01-12 16:08:10 +000045use std::io::{Read, Write};
46use std::os::unix::fs::PermissionsExt;
47use std::os::unix::raw::{pid_t, uid_t};
Inseob Kim55438b22023-08-09 20:16:01 +090048use std::path::{Path, PathBuf};
David Brazdilafc9a9e2023-01-12 16:08:10 +000049use std::sync::{Arc, Mutex, Weak};
50use tombstoned_client::{DebuggerdDumpType, TombstonedConnection};
51use vsock::{VsockListener, VsockStream};
Inseob Kimbdca0472023-07-28 19:20:56 +090052use nix::unistd::{chown, Uid};
David Brazdilafc9a9e2023-01-12 16:08:10 +000053
54/// The unique ID of a VM used (together with a port number) for vsock communication.
55pub type Cid = u32;
56
57pub const BINDER_SERVICE_IDENTIFIER: &str = "android.system.virtualizationservice";
58
59/// Directory in which to write disk image files used while running VMs.
60pub const TEMPORARY_DIRECTORY: &str = "/data/misc/virtualizationservice";
61
62/// The first CID to assign to a guest VM managed by the VirtualizationService. CIDs lower than this
63/// are reserved for the host or other usage.
64const GUEST_CID_MIN: Cid = 2048;
65const GUEST_CID_MAX: Cid = 65535;
66
67const SYSPROP_LAST_CID: &str = "virtualizationservice.state.last_cid";
68
69const CHUNK_RECV_MAX_LEN: usize = 1024;
70
71fn is_valid_guest_cid(cid: Cid) -> bool {
72 (GUEST_CID_MIN..=GUEST_CID_MAX).contains(&cid)
73}
74
75/// Singleton service for allocating globally-unique VM resources, such as the CID, and running
76/// singleton servers, like tombstone receiver.
77#[derive(Debug, Default)]
78pub struct VirtualizationServiceInternal {
79 state: Arc<Mutex<GlobalState>>,
80}
81
82impl VirtualizationServiceInternal {
83 pub fn init() -> VirtualizationServiceInternal {
84 let service = VirtualizationServiceInternal::default();
85
86 std::thread::spawn(|| {
87 if let Err(e) = handle_stream_connection_tombstoned() {
88 warn!("Error receiving tombstone from guest or writing them. Error: {:?}", e);
89 }
90 });
91
92 service
93 }
94}
95
96impl Interface for VirtualizationServiceInternal {}
97
98impl IVirtualizationServiceInternal for VirtualizationServiceInternal {
99 fn removeMemlockRlimit(&self) -> binder::Result<()> {
100 let pid = get_calling_pid();
101 let lim = libc::rlimit { rlim_cur: libc::RLIM_INFINITY, rlim_max: libc::RLIM_INFINITY };
102
Andrew Walbranb58d1b42023-07-07 13:54:49 +0100103 // SAFETY: borrowing the new limit struct only
David Brazdilafc9a9e2023-01-12 16:08:10 +0000104 let ret = unsafe { libc::prlimit(pid, libc::RLIMIT_MEMLOCK, &lim, std::ptr::null_mut()) };
105
106 match ret {
107 0 => Ok(()),
Jiyong Park2227eaa2023-08-04 11:59:18 +0900108 -1 => Err(std::io::Error::last_os_error().into()),
109 n => Err(anyhow!("Unexpected return value from prlimit(): {n}")),
David Brazdilafc9a9e2023-01-12 16:08:10 +0000110 }
Jiyong Park2227eaa2023-08-04 11:59:18 +0900111 .or_binder_exception(ExceptionCode::ILLEGAL_STATE)
David Brazdilafc9a9e2023-01-12 16:08:10 +0000112 }
113
114 fn allocateGlobalVmContext(
115 &self,
116 requester_debug_pid: i32,
117 ) -> binder::Result<Strong<dyn IGlobalVmContext>> {
118 check_manage_access()?;
119
120 let requester_uid = get_calling_uid();
121 let requester_debug_pid = requester_debug_pid as pid_t;
122 let state = &mut *self.state.lock().unwrap();
Jiyong Park2227eaa2023-08-04 11:59:18 +0900123 state
124 .allocate_vm_context(requester_uid, requester_debug_pid)
125 .or_binder_exception(ExceptionCode::ILLEGAL_STATE)
David Brazdilafc9a9e2023-01-12 16:08:10 +0000126 }
127
128 fn atomVmBooted(&self, atom: &AtomVmBooted) -> Result<(), Status> {
129 forward_vm_booted_atom(atom);
130 Ok(())
131 }
132
133 fn atomVmCreationRequested(&self, atom: &AtomVmCreationRequested) -> Result<(), Status> {
134 forward_vm_creation_atom(atom);
135 Ok(())
136 }
137
138 fn atomVmExited(&self, atom: &AtomVmExited) -> Result<(), Status> {
139 forward_vm_exited_atom(atom);
140 Ok(())
141 }
142
143 fn debugListVms(&self) -> binder::Result<Vec<VirtualMachineDebugInfo>> {
144 check_debug_access()?;
145
146 let state = &mut *self.state.lock().unwrap();
147 let cids = state
148 .held_contexts
149 .iter()
150 .filter_map(|(_, inst)| Weak::upgrade(inst))
151 .map(|vm| VirtualMachineDebugInfo {
152 cid: vm.cid as i32,
153 temporaryDirectory: vm.get_temp_dir().to_string_lossy().to_string(),
154 requesterUid: vm.requester_uid as i32,
Charisee96113f32023-01-26 09:00:42 +0000155 requesterPid: vm.requester_debug_pid,
David Brazdilafc9a9e2023-01-12 16:08:10 +0000156 })
157 .collect();
158 Ok(cids)
159 }
Alice Wangc2fec932023-02-23 16:24:02 +0000160
Alice Wanga410b642023-10-18 09:05:15 +0000161 fn requestAttestation(&self, csr: &[u8]) -> binder::Result<Vec<u8>> {
Alice Wangc2fec932023-02-23 16:24:02 +0000162 check_manage_access()?;
Alice Wanga410b642023-10-18 09:05:15 +0000163 info!("Received csr. Requestting attestation...");
Alice Wange9ac2db2023-09-08 15:13:13 +0000164 if cfg!(remote_attestation) {
Alice Wanga410b642023-10-18 09:05:15 +0000165 request_attestation(csr)
166 .context("Failed to request attestation")
Alice Wange9ac2db2023-09-08 15:13:13 +0000167 .with_log()
168 .or_service_specific_exception(-1)
169 } else {
170 Err(Status::new_exception_str(
171 ExceptionCode::UNSUPPORTED_OPERATION,
172 Some(
Alice Wanga410b642023-10-18 09:05:15 +0000173 "requestAttestation is not supported with the remote_attestation feature \
174 disabled",
Alice Wange9ac2db2023-09-08 15:13:13 +0000175 ),
176 ))
Jiyong Park2227eaa2023-08-04 11:59:18 +0900177 .with_log()
Alice Wange9ac2db2023-09-08 15:13:13 +0000178 }
Alice Wangc2fec932023-02-23 16:24:02 +0000179 }
Inseob Kim53d0b212023-07-20 16:58:37 +0900180
181 fn getAssignableDevices(&self) -> binder::Result<Vec<AssignableDevice>> {
182 check_use_custom_virtual_machine()?;
183
Inseob Kim7307a892023-09-14 13:37:58 +0900184 Ok(get_assignable_devices()?
185 .device
186 .into_iter()
187 .map(|x| AssignableDevice { node: x.sysfs_path, kind: x.kind })
188 .collect::<Vec<_>>())
Inseob Kim53d0b212023-07-20 16:58:37 +0900189 }
Inseob Kim1ca0f652023-07-20 17:18:12 +0900190
Inseob Kim7307a892023-09-14 13:37:58 +0900191 fn bindDevicesToVfioDriver(&self, devices: &[String]) -> binder::Result<Vec<BoundDevice>> {
Inseob Kim1ca0f652023-07-20 17:18:12 +0900192 check_use_custom_virtual_machine()?;
193
Inseob Kimbdca0472023-07-28 19:20:56 +0900194 let vfio_service: Strong<dyn IVfioHandler> =
195 wait_for_interface(<BpVfioHandler as IVfioHandler>::get_descriptor())?;
Inseob Kimf36347b2023-08-03 12:52:48 +0900196
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +0900197 vfio_service.bindDevicesToVfioDriver(devices)?;
198
199 let dtbo_path = Path::new(TEMPORARY_DIRECTORY).join("common").join("dtbo");
200 if !dtbo_path.exists() {
201 // open a writable file descriptor for vfio_handler
202 let dtbo = File::create(&dtbo_path)
203 .context("Failed to create VM DTBO file")
204 .or_service_specific_exception(-1)?;
205 vfio_service.writeVmDtbo(&ParcelFileDescriptor::new(dtbo))?;
206 }
207
Inseob Kim7307a892023-09-14 13:37:58 +0900208 Ok(get_assignable_devices()?
209 .device
210 .into_iter()
211 .filter_map(|x| {
212 if devices.contains(&x.sysfs_path) {
Jaewan Kim35e818d2023-10-18 05:36:38 +0000213 Some(BoundDevice { sysfsPath: x.sysfs_path, dtboLabel: x.dtbo_label })
Inseob Kim7307a892023-09-14 13:37:58 +0900214 } else {
215 None
216 }
217 })
218 .collect::<Vec<_>>())
Inseob Kim1ca0f652023-07-20 17:18:12 +0900219 }
220}
221
Inseob Kimc4a774d2023-08-30 12:48:43 +0900222// KEEP IN SYNC WITH assignable_devices.xsd
223#[derive(Debug, Deserialize)]
224struct Device {
225 kind: String,
Jaewan Kim35e818d2023-10-18 05:36:38 +0000226 dtbo_label: String,
Inseob Kimc4a774d2023-08-30 12:48:43 +0900227 sysfs_path: String,
228}
229
Inseob Kim7307a892023-09-14 13:37:58 +0900230#[derive(Debug, Default, Deserialize)]
Inseob Kimc4a774d2023-08-30 12:48:43 +0900231struct Devices {
232 device: Vec<Device>,
233}
234
Inseob Kim7307a892023-09-14 13:37:58 +0900235fn get_assignable_devices() -> binder::Result<Devices> {
236 let xml_path = Path::new("/vendor/etc/avf/assignable_devices.xml");
237 if !xml_path.exists() {
238 return Ok(Devices { ..Default::default() });
239 }
240
241 let xml = fs::read(xml_path)
242 .context("Failed to read assignable_devices.xml")
243 .with_log()
244 .or_service_specific_exception(-1)?;
245
246 let xml = String::from_utf8(xml)
247 .context("assignable_devices.xml is not a valid UTF-8 file")
248 .with_log()
249 .or_service_specific_exception(-1)?;
250
251 let mut devices: Devices = serde_xml_rs::from_str(&xml)
252 .context("can't parse assignable_devices.xml")
253 .with_log()
254 .or_service_specific_exception(-1)?;
255
256 let mut device_set = HashSet::new();
257 devices.device.retain(move |device| {
258 if device_set.contains(&device.sysfs_path) {
259 warn!("duplicated assignable device {device:?}; ignoring...");
260 return false;
261 }
262
263 if !Path::new(&device.sysfs_path).exists() {
264 warn!("assignable device {device:?} doesn't exist; ignoring...");
265 return false;
266 }
267
268 device_set.insert(device.sysfs_path.clone());
269 true
270 });
271 Ok(devices)
272}
273
David Brazdilafc9a9e2023-01-12 16:08:10 +0000274#[derive(Debug, Default)]
275struct GlobalVmInstance {
276 /// The unique CID assigned to the VM for vsock communication.
277 cid: Cid,
278 /// UID of the client who requested this VM instance.
279 requester_uid: uid_t,
280 /// PID of the client who requested this VM instance.
281 requester_debug_pid: pid_t,
282}
283
284impl GlobalVmInstance {
285 fn get_temp_dir(&self) -> PathBuf {
286 let cid = self.cid;
287 format!("{TEMPORARY_DIRECTORY}/{cid}").into()
288 }
289}
290
291/// The mutable state of the VirtualizationServiceInternal. There should only be one instance
292/// of this struct.
293#[derive(Debug, Default)]
294struct GlobalState {
295 /// VM contexts currently allocated to running VMs. A CID is never recycled as long
296 /// as there is a strong reference held by a GlobalVmContext.
297 held_contexts: HashMap<Cid, Weak<GlobalVmInstance>>,
298}
299
300impl GlobalState {
301 /// Get the next available CID, or an error if we have run out. The last CID used is stored in
302 /// a system property so that restart of virtualizationservice doesn't reuse CID while the host
303 /// Android is up.
304 fn get_next_available_cid(&mut self) -> Result<Cid> {
305 // Start trying to find a CID from the last used CID + 1. This ensures
306 // that we do not eagerly recycle CIDs. It makes debugging easier but
307 // also means that retrying to allocate a CID, eg. because it is
308 // erroneously occupied by a process, will not recycle the same CID.
309 let last_cid_prop =
310 system_properties::read(SYSPROP_LAST_CID)?.and_then(|val| match val.parse::<Cid>() {
311 Ok(num) => {
312 if is_valid_guest_cid(num) {
313 Some(num)
314 } else {
315 error!("Invalid value '{}' of property '{}'", num, SYSPROP_LAST_CID);
316 None
317 }
318 }
319 Err(_) => {
320 error!("Invalid value '{}' of property '{}'", val, SYSPROP_LAST_CID);
321 None
322 }
323 });
324
325 let first_cid = if let Some(last_cid) = last_cid_prop {
326 if last_cid == GUEST_CID_MAX {
327 GUEST_CID_MIN
328 } else {
329 last_cid + 1
330 }
331 } else {
332 GUEST_CID_MIN
333 };
334
335 let cid = self
336 .find_available_cid(first_cid..=GUEST_CID_MAX)
337 .or_else(|| self.find_available_cid(GUEST_CID_MIN..first_cid))
338 .ok_or_else(|| anyhow!("Could not find an available CID."))?;
339
340 system_properties::write(SYSPROP_LAST_CID, &format!("{}", cid))?;
341 Ok(cid)
342 }
343
344 fn find_available_cid<I>(&self, mut range: I) -> Option<Cid>
345 where
346 I: Iterator<Item = Cid>,
347 {
348 range.find(|cid| !self.held_contexts.contains_key(cid))
349 }
350
351 fn allocate_vm_context(
352 &mut self,
353 requester_uid: uid_t,
354 requester_debug_pid: pid_t,
355 ) -> Result<Strong<dyn IGlobalVmContext>> {
356 // Garbage collect unused VM contexts.
357 self.held_contexts.retain(|_, instance| instance.strong_count() > 0);
358
359 let cid = self.get_next_available_cid()?;
360 let instance = Arc::new(GlobalVmInstance { cid, requester_uid, requester_debug_pid });
361 create_temporary_directory(&instance.get_temp_dir(), requester_uid)?;
362
363 self.held_contexts.insert(cid, Arc::downgrade(&instance));
364 let binder = GlobalVmContext { instance, ..Default::default() };
365 Ok(BnGlobalVmContext::new_binder(binder, BinderFeatures::default()))
366 }
367}
368
369fn create_temporary_directory(path: &PathBuf, requester_uid: uid_t) -> Result<()> {
370 if path.as_path().exists() {
371 remove_temporary_dir(path).unwrap_or_else(|e| {
372 warn!("Could not delete temporary directory {:?}: {}", path, e);
373 });
374 }
375 // Create a directory that is owned by client's UID but system's GID, and permissions 0700.
376 // If the chown() fails, this will leave behind an empty directory that will get removed
377 // at the next attempt, or if virtualizationservice is restarted.
378 create_dir(path).with_context(|| format!("Could not create temporary directory {:?}", path))?;
379 chown(path, Some(Uid::from_raw(requester_uid)), None)
380 .with_context(|| format!("Could not set ownership of temporary directory {:?}", path))?;
381 Ok(())
382}
383
384/// Removes a directory owned by a different user by first changing its owner back
385/// to VirtualizationService.
386pub fn remove_temporary_dir(path: &PathBuf) -> Result<()> {
Alice Wangd1b11a02023-04-18 12:30:20 +0000387 ensure!(path.as_path().is_dir(), "Path {:?} is not a directory", path);
David Brazdilafc9a9e2023-01-12 16:08:10 +0000388 chown(path, Some(Uid::current()), None)?;
389 set_permissions(path, Permissions::from_mode(0o700))?;
Alice Wangd1b11a02023-04-18 12:30:20 +0000390 remove_dir_all(path)?;
David Brazdilafc9a9e2023-01-12 16:08:10 +0000391 Ok(())
392}
393
394/// Implementation of the AIDL `IGlobalVmContext` interface.
395#[derive(Debug, Default)]
396struct GlobalVmContext {
397 /// Strong reference to the context's instance data structure.
398 instance: Arc<GlobalVmInstance>,
399 /// Keeps our service process running as long as this VM context exists.
400 #[allow(dead_code)]
401 lazy_service_guard: LazyServiceGuard,
402}
403
404impl Interface for GlobalVmContext {}
405
406impl IGlobalVmContext for GlobalVmContext {
407 fn getCid(&self) -> binder::Result<i32> {
408 Ok(self.instance.cid as i32)
409 }
410
411 fn getTemporaryDirectory(&self) -> binder::Result<String> {
412 Ok(self.instance.get_temp_dir().to_string_lossy().to_string())
413 }
414}
415
416fn handle_stream_connection_tombstoned() -> Result<()> {
417 // Should not listen for tombstones on a guest VM's port.
418 assert!(!is_valid_guest_cid(VM_TOMBSTONES_SERVICE_PORT as Cid));
419 let listener =
420 VsockListener::bind_with_cid_port(VMADDR_CID_HOST, VM_TOMBSTONES_SERVICE_PORT as Cid)?;
421 for incoming_stream in listener.incoming() {
422 let mut incoming_stream = match incoming_stream {
423 Err(e) => {
424 warn!("invalid incoming connection: {:?}", e);
425 continue;
426 }
427 Ok(s) => s,
428 };
429 std::thread::spawn(move || {
430 if let Err(e) = handle_tombstone(&mut incoming_stream) {
431 error!("Failed to write tombstone- {:?}", e);
432 }
433 });
434 }
435 Ok(())
436}
437
438fn handle_tombstone(stream: &mut VsockStream) -> Result<()> {
439 if let Ok(addr) = stream.peer_addr() {
440 info!("Vsock Stream connected to cid={} for tombstones", addr.cid());
441 }
442 let tb_connection =
443 TombstonedConnection::connect(std::process::id() as i32, DebuggerdDumpType::Tombstone)
444 .context("Failed to connect to tombstoned")?;
445 let mut text_output = tb_connection
446 .text_output
447 .as_ref()
448 .ok_or_else(|| anyhow!("Could not get file to write the tombstones on"))?;
449 let mut num_bytes_read = 0;
450 loop {
451 let mut chunk_recv = [0; CHUNK_RECV_MAX_LEN];
452 let n = stream
453 .read(&mut chunk_recv)
454 .context("Failed to read tombstone data from Vsock stream")?;
455 if n == 0 {
456 break;
457 }
458 num_bytes_read += n;
459 text_output.write_all(&chunk_recv[0..n]).context("Failed to write guests tombstones")?;
460 }
461 info!("Received {} bytes from guest & wrote to tombstone file", num_bytes_read);
462 tb_connection.notify_completion()?;
463 Ok(())
464}
465
466/// Checks whether the caller has a specific permission
467fn check_permission(perm: &str) -> binder::Result<()> {
468 let calling_pid = get_calling_pid();
469 let calling_uid = get_calling_uid();
470 // Root can do anything
471 if calling_uid == 0 {
472 return Ok(());
473 }
474 let perm_svc: Strong<dyn IPermissionController::IPermissionController> =
475 binder::get_interface("permission")?;
476 if perm_svc.checkPermission(perm, calling_pid, calling_uid as i32)? {
477 Ok(())
478 } else {
Jiyong Park2227eaa2023-08-04 11:59:18 +0900479 Err(anyhow!("does not have the {} permission", perm))
480 .or_binder_exception(ExceptionCode::SECURITY)
David Brazdilafc9a9e2023-01-12 16:08:10 +0000481 }
482}
483
484/// Check whether the caller of the current Binder method is allowed to call debug methods.
485fn check_debug_access() -> binder::Result<()> {
486 check_permission("android.permission.DEBUG_VIRTUAL_MACHINE")
487}
488
489/// Check whether the caller of the current Binder method is allowed to manage VMs
490fn check_manage_access() -> binder::Result<()> {
491 check_permission("android.permission.MANAGE_VIRTUAL_MACHINE")
492}
Inseob Kim53d0b212023-07-20 16:58:37 +0900493
494/// Check whether the caller of the current Binder method is allowed to use custom VMs
495fn check_use_custom_virtual_machine() -> binder::Result<()> {
496 check_permission("android.permission.USE_CUSTOM_VIRTUAL_MACHINE")
497}