blob: 3ac1e605d1048a81a66d5325b316e4cecc704db2 [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
Alice Wangbff017f2023-11-09 14:43:28 +000017use crate::{get_calling_pid, get_calling_uid, REMOTELY_PROVISIONED_COMPONENT_SERVICE_NAME};
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 Wang4e3015d2023-10-10 09:35:37 +000021use android_system_virtualizationcommon::aidl::android::system::virtualizationcommon::Certificate::Certificate;
Alice Wangc2fec932023-02-23 16:24:02 +000022use android_system_virtualizationservice::{
Inseob Kim53d0b212023-07-20 16:58:37 +090023 aidl::android::system::virtualizationservice::AssignableDevice::AssignableDevice,
Alice Wangc2fec932023-02-23 16:24:02 +000024 aidl::android::system::virtualizationservice::VirtualMachineDebugInfo::VirtualMachineDebugInfo,
25 binder::ParcelFileDescriptor,
26};
David Brazdilafc9a9e2023-01-12 16:08:10 +000027use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::{
28 AtomVmBooted::AtomVmBooted,
29 AtomVmCreationRequested::AtomVmCreationRequested,
30 AtomVmExited::AtomVmExited,
31 IGlobalVmContext::{BnGlobalVmContext, IGlobalVmContext},
Inseob Kim7307a892023-09-14 13:37:58 +090032 IVirtualizationServiceInternal::BoundDevice::BoundDevice,
David Brazdilafc9a9e2023-01-12 16:08:10 +000033 IVirtualizationServiceInternal::IVirtualizationServiceInternal,
Inseob Kimbdca0472023-07-28 19:20:56 +090034 IVfioHandler::{BpVfioHandler, IVfioHandler},
David Brazdilafc9a9e2023-01-12 16:08:10 +000035};
36use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::VM_TOMBSTONES_SERVICE_PORT;
Alice Wangd1b11a02023-04-18 12:30:20 +000037use anyhow::{anyhow, ensure, Context, Result};
Jiyong Parkd7bd2f22023-08-10 20:41:19 +090038use avflog::LogResult;
Jiyong Park2227eaa2023-08-04 11:59:18 +090039use binder::{self, wait_for_interface, BinderFeatures, ExceptionCode, Interface, LazyServiceGuard, Status, Strong, IntoBinderResult};
David Brazdilafc9a9e2023-01-12 16:08:10 +000040use libc::VMADDR_CID_HOST;
41use log::{error, info, warn};
Alice Wangbff017f2023-11-09 14:43:28 +000042use rkpd_client::get_rkpd_attestation_key;
David Brazdilafc9a9e2023-01-12 16:08:10 +000043use rustutils::system_properties;
Inseob Kimc4a774d2023-08-30 12:48:43 +090044use serde::Deserialize;
45use std::collections::{HashMap, HashSet};
David Brazdil2dfefd12023-11-17 14:07:36 +000046use std::fs::{self, create_dir, remove_dir_all, remove_file, set_permissions, File, Permissions};
David Brazdilafc9a9e2023-01-12 16:08:10 +000047use std::io::{Read, Write};
48use std::os::unix::fs::PermissionsExt;
49use std::os::unix::raw::{pid_t, uid_t};
Inseob Kim55438b22023-08-09 20:16:01 +090050use std::path::{Path, PathBuf};
David Brazdilafc9a9e2023-01-12 16:08:10 +000051use std::sync::{Arc, Mutex, Weak};
52use tombstoned_client::{DebuggerdDumpType, TombstonedConnection};
53use vsock::{VsockListener, VsockStream};
Inseob Kimbdca0472023-07-28 19:20:56 +090054use nix::unistd::{chown, Uid};
Alice Wang4c6c5582023-11-23 15:07:18 +000055use x509_parser::{traits::FromDer, certificate::X509Certificate};
David Brazdilafc9a9e2023-01-12 16:08:10 +000056
57/// The unique ID of a VM used (together with a port number) for vsock communication.
58pub type Cid = u32;
59
60pub const BINDER_SERVICE_IDENTIFIER: &str = "android.system.virtualizationservice";
61
62/// Directory in which to write disk image files used while running VMs.
63pub const TEMPORARY_DIRECTORY: &str = "/data/misc/virtualizationservice";
64
65/// The first CID to assign to a guest VM managed by the VirtualizationService. CIDs lower than this
66/// are reserved for the host or other usage.
67const GUEST_CID_MIN: Cid = 2048;
68const GUEST_CID_MAX: Cid = 65535;
69
70const SYSPROP_LAST_CID: &str = "virtualizationservice.state.last_cid";
71
72const CHUNK_RECV_MAX_LEN: usize = 1024;
73
74fn is_valid_guest_cid(cid: Cid) -> bool {
75 (GUEST_CID_MIN..=GUEST_CID_MAX).contains(&cid)
76}
77
78/// Singleton service for allocating globally-unique VM resources, such as the CID, and running
79/// singleton servers, like tombstone receiver.
80#[derive(Debug, Default)]
81pub struct VirtualizationServiceInternal {
82 state: Arc<Mutex<GlobalState>>,
83}
84
85impl VirtualizationServiceInternal {
86 pub fn init() -> VirtualizationServiceInternal {
87 let service = VirtualizationServiceInternal::default();
88
89 std::thread::spawn(|| {
90 if let Err(e) = handle_stream_connection_tombstoned() {
91 warn!("Error receiving tombstone from guest or writing them. Error: {:?}", e);
92 }
93 });
94
95 service
96 }
97}
98
99impl Interface for VirtualizationServiceInternal {}
100
101impl IVirtualizationServiceInternal for VirtualizationServiceInternal {
102 fn removeMemlockRlimit(&self) -> binder::Result<()> {
103 let pid = get_calling_pid();
104 let lim = libc::rlimit { rlim_cur: libc::RLIM_INFINITY, rlim_max: libc::RLIM_INFINITY };
105
Andrew Walbranb58d1b42023-07-07 13:54:49 +0100106 // SAFETY: borrowing the new limit struct only
David Brazdilafc9a9e2023-01-12 16:08:10 +0000107 let ret = unsafe { libc::prlimit(pid, libc::RLIMIT_MEMLOCK, &lim, std::ptr::null_mut()) };
108
109 match ret {
110 0 => Ok(()),
Jiyong Park2227eaa2023-08-04 11:59:18 +0900111 -1 => Err(std::io::Error::last_os_error().into()),
112 n => Err(anyhow!("Unexpected return value from prlimit(): {n}")),
David Brazdilafc9a9e2023-01-12 16:08:10 +0000113 }
Jiyong Park2227eaa2023-08-04 11:59:18 +0900114 .or_binder_exception(ExceptionCode::ILLEGAL_STATE)
David Brazdilafc9a9e2023-01-12 16:08:10 +0000115 }
116
117 fn allocateGlobalVmContext(
118 &self,
119 requester_debug_pid: i32,
120 ) -> binder::Result<Strong<dyn IGlobalVmContext>> {
121 check_manage_access()?;
122
123 let requester_uid = get_calling_uid();
124 let requester_debug_pid = requester_debug_pid as pid_t;
125 let state = &mut *self.state.lock().unwrap();
Jiyong Park2227eaa2023-08-04 11:59:18 +0900126 state
127 .allocate_vm_context(requester_uid, requester_debug_pid)
128 .or_binder_exception(ExceptionCode::ILLEGAL_STATE)
David Brazdilafc9a9e2023-01-12 16:08:10 +0000129 }
130
131 fn atomVmBooted(&self, atom: &AtomVmBooted) -> Result<(), Status> {
132 forward_vm_booted_atom(atom);
133 Ok(())
134 }
135
136 fn atomVmCreationRequested(&self, atom: &AtomVmCreationRequested) -> Result<(), Status> {
137 forward_vm_creation_atom(atom);
138 Ok(())
139 }
140
141 fn atomVmExited(&self, atom: &AtomVmExited) -> Result<(), Status> {
142 forward_vm_exited_atom(atom);
143 Ok(())
144 }
145
146 fn debugListVms(&self) -> binder::Result<Vec<VirtualMachineDebugInfo>> {
147 check_debug_access()?;
148
149 let state = &mut *self.state.lock().unwrap();
150 let cids = state
151 .held_contexts
152 .iter()
153 .filter_map(|(_, inst)| Weak::upgrade(inst))
154 .map(|vm| VirtualMachineDebugInfo {
155 cid: vm.cid as i32,
156 temporaryDirectory: vm.get_temp_dir().to_string_lossy().to_string(),
157 requesterUid: vm.requester_uid as i32,
Charisee96113f32023-01-26 09:00:42 +0000158 requesterPid: vm.requester_debug_pid,
David Brazdilafc9a9e2023-01-12 16:08:10 +0000159 })
160 .collect();
161 Ok(cids)
162 }
Alice Wangc2fec932023-02-23 16:24:02 +0000163
Alice Wangbff017f2023-11-09 14:43:28 +0000164 fn requestAttestation(
165 &self,
166 csr: &[u8],
167 requester_uid: i32,
168 ) -> binder::Result<Vec<Certificate>> {
Alice Wangc2fec932023-02-23 16:24:02 +0000169 check_manage_access()?;
Alice Wang4c6c5582023-11-23 15:07:18 +0000170 if !cfg!(remote_attestation) {
171 return Err(Status::new_exception_str(
Alice Wange9ac2db2023-09-08 15:13:13 +0000172 ExceptionCode::UNSUPPORTED_OPERATION,
173 Some(
Alice Wanga410b642023-10-18 09:05:15 +0000174 "requestAttestation is not supported with the remote_attestation feature \
175 disabled",
Alice Wange9ac2db2023-09-08 15:13:13 +0000176 ),
177 ))
Alice Wang4c6c5582023-11-23 15:07:18 +0000178 .with_log();
Alice Wange9ac2db2023-09-08 15:13:13 +0000179 }
Alice Wang4c6c5582023-11-23 15:07:18 +0000180 info!("Received csr. Requestting attestation...");
181 let attestation_key = get_rkpd_attestation_key(
182 REMOTELY_PROVISIONED_COMPONENT_SERVICE_NAME,
183 requester_uid as u32,
184 )
185 .context("Failed to retrieve the remotely provisioned keys")
186 .with_log()
187 .or_service_specific_exception(-1)?;
188 let mut certificate_chain = split_x509_certificate_chain(&attestation_key.encodedCertChain)
189 .context("Failed to split the remotely provisioned certificate chain")
190 .with_log()
191 .or_service_specific_exception(-1)?;
192 if certificate_chain.is_empty() {
193 return Err(Status::new_service_specific_error_str(
194 -1,
195 Some("The certificate chain should contain at least 1 certificate"),
196 ))
197 .with_log();
198 }
Alice Wang20b8ebc2023-11-17 09:54:47 +0000199 let certificate = request_attestation(
200 csr.to_vec(),
201 attestation_key.keyBlob,
202 certificate_chain[0].encodedCertificate.clone(),
203 )
204 .context("Failed to request attestation")
205 .with_log()
206 .or_service_specific_exception(-1)?;
Alice Wang4c6c5582023-11-23 15:07:18 +0000207 certificate_chain.insert(0, Certificate { encodedCertificate: certificate });
208
209 Ok(certificate_chain)
Alice Wangc2fec932023-02-23 16:24:02 +0000210 }
Inseob Kim53d0b212023-07-20 16:58:37 +0900211
212 fn getAssignableDevices(&self) -> binder::Result<Vec<AssignableDevice>> {
213 check_use_custom_virtual_machine()?;
214
Inseob Kim7307a892023-09-14 13:37:58 +0900215 Ok(get_assignable_devices()?
216 .device
217 .into_iter()
218 .map(|x| AssignableDevice { node: x.sysfs_path, kind: x.kind })
219 .collect::<Vec<_>>())
Inseob Kim53d0b212023-07-20 16:58:37 +0900220 }
Inseob Kim1ca0f652023-07-20 17:18:12 +0900221
Inseob Kim7307a892023-09-14 13:37:58 +0900222 fn bindDevicesToVfioDriver(&self, devices: &[String]) -> binder::Result<Vec<BoundDevice>> {
Inseob Kim1ca0f652023-07-20 17:18:12 +0900223 check_use_custom_virtual_machine()?;
224
Inseob Kimbdca0472023-07-28 19:20:56 +0900225 let vfio_service: Strong<dyn IVfioHandler> =
226 wait_for_interface(<BpVfioHandler as IVfioHandler>::get_descriptor())?;
Seungjae Yoo9d3c20a2023-09-07 15:36:44 +0900227 vfio_service.bindDevicesToVfioDriver(devices)?;
228
Inseob Kim7307a892023-09-14 13:37:58 +0900229 Ok(get_assignable_devices()?
230 .device
231 .into_iter()
232 .filter_map(|x| {
233 if devices.contains(&x.sysfs_path) {
Jaewan Kim35e818d2023-10-18 05:36:38 +0000234 Some(BoundDevice { sysfsPath: x.sysfs_path, dtboLabel: x.dtbo_label })
Inseob Kim7307a892023-09-14 13:37:58 +0900235 } else {
236 None
237 }
238 })
239 .collect::<Vec<_>>())
Inseob Kim1ca0f652023-07-20 17:18:12 +0900240 }
David Brazdil2dfefd12023-11-17 14:07:36 +0000241
242 fn getDtboFile(&self) -> binder::Result<ParcelFileDescriptor> {
243 check_use_custom_virtual_machine()?;
244
245 let state = &mut *self.state.lock().unwrap();
246 let file = state.get_dtbo_file().or_service_specific_exception(-1)?;
247 Ok(ParcelFileDescriptor::new(file))
248 }
Inseob Kim1ca0f652023-07-20 17:18:12 +0900249}
250
Inseob Kimc4a774d2023-08-30 12:48:43 +0900251// KEEP IN SYNC WITH assignable_devices.xsd
252#[derive(Debug, Deserialize)]
253struct Device {
254 kind: String,
Jaewan Kim35e818d2023-10-18 05:36:38 +0000255 dtbo_label: String,
Inseob Kimc4a774d2023-08-30 12:48:43 +0900256 sysfs_path: String,
257}
258
Inseob Kim7307a892023-09-14 13:37:58 +0900259#[derive(Debug, Default, Deserialize)]
Inseob Kimc4a774d2023-08-30 12:48:43 +0900260struct Devices {
261 device: Vec<Device>,
262}
263
Inseob Kim7307a892023-09-14 13:37:58 +0900264fn get_assignable_devices() -> binder::Result<Devices> {
265 let xml_path = Path::new("/vendor/etc/avf/assignable_devices.xml");
266 if !xml_path.exists() {
267 return Ok(Devices { ..Default::default() });
268 }
269
270 let xml = fs::read(xml_path)
271 .context("Failed to read assignable_devices.xml")
272 .with_log()
273 .or_service_specific_exception(-1)?;
274
275 let xml = String::from_utf8(xml)
276 .context("assignable_devices.xml is not a valid UTF-8 file")
277 .with_log()
278 .or_service_specific_exception(-1)?;
279
280 let mut devices: Devices = serde_xml_rs::from_str(&xml)
281 .context("can't parse assignable_devices.xml")
282 .with_log()
283 .or_service_specific_exception(-1)?;
284
285 let mut device_set = HashSet::new();
286 devices.device.retain(move |device| {
287 if device_set.contains(&device.sysfs_path) {
288 warn!("duplicated assignable device {device:?}; ignoring...");
289 return false;
290 }
291
292 if !Path::new(&device.sysfs_path).exists() {
293 warn!("assignable device {device:?} doesn't exist; ignoring...");
294 return false;
295 }
296
297 device_set.insert(device.sysfs_path.clone());
298 true
299 });
300 Ok(devices)
301}
302
Alice Wang4c6c5582023-11-23 15:07:18 +0000303fn split_x509_certificate_chain(mut cert_chain: &[u8]) -> Result<Vec<Certificate>> {
304 let mut out = Vec::new();
305 while !cert_chain.is_empty() {
306 let (remaining, _) = X509Certificate::from_der(cert_chain)?;
307 let end = cert_chain.len() - remaining.len();
308 out.push(Certificate { encodedCertificate: cert_chain[..end].to_vec() });
309 cert_chain = remaining;
310 }
311 Ok(out)
312}
313
David Brazdilafc9a9e2023-01-12 16:08:10 +0000314#[derive(Debug, Default)]
315struct GlobalVmInstance {
316 /// The unique CID assigned to the VM for vsock communication.
317 cid: Cid,
318 /// UID of the client who requested this VM instance.
319 requester_uid: uid_t,
320 /// PID of the client who requested this VM instance.
321 requester_debug_pid: pid_t,
322}
323
324impl GlobalVmInstance {
325 fn get_temp_dir(&self) -> PathBuf {
326 let cid = self.cid;
327 format!("{TEMPORARY_DIRECTORY}/{cid}").into()
328 }
329}
330
331/// The mutable state of the VirtualizationServiceInternal. There should only be one instance
332/// of this struct.
333#[derive(Debug, Default)]
334struct GlobalState {
335 /// VM contexts currently allocated to running VMs. A CID is never recycled as long
336 /// as there is a strong reference held by a GlobalVmContext.
337 held_contexts: HashMap<Cid, Weak<GlobalVmInstance>>,
David Brazdil2dfefd12023-11-17 14:07:36 +0000338
339 /// Cached read-only FD of VM DTBO file. Also serves as a lock for creating the file.
340 dtbo_file: Mutex<Option<File>>,
David Brazdilafc9a9e2023-01-12 16:08:10 +0000341}
342
343impl GlobalState {
344 /// Get the next available CID, or an error if we have run out. The last CID used is stored in
345 /// a system property so that restart of virtualizationservice doesn't reuse CID while the host
346 /// Android is up.
347 fn get_next_available_cid(&mut self) -> Result<Cid> {
348 // Start trying to find a CID from the last used CID + 1. This ensures
349 // that we do not eagerly recycle CIDs. It makes debugging easier but
350 // also means that retrying to allocate a CID, eg. because it is
351 // erroneously occupied by a process, will not recycle the same CID.
352 let last_cid_prop =
353 system_properties::read(SYSPROP_LAST_CID)?.and_then(|val| match val.parse::<Cid>() {
354 Ok(num) => {
355 if is_valid_guest_cid(num) {
356 Some(num)
357 } else {
358 error!("Invalid value '{}' of property '{}'", num, SYSPROP_LAST_CID);
359 None
360 }
361 }
362 Err(_) => {
363 error!("Invalid value '{}' of property '{}'", val, SYSPROP_LAST_CID);
364 None
365 }
366 });
367
368 let first_cid = if let Some(last_cid) = last_cid_prop {
369 if last_cid == GUEST_CID_MAX {
370 GUEST_CID_MIN
371 } else {
372 last_cid + 1
373 }
374 } else {
375 GUEST_CID_MIN
376 };
377
378 let cid = self
379 .find_available_cid(first_cid..=GUEST_CID_MAX)
380 .or_else(|| self.find_available_cid(GUEST_CID_MIN..first_cid))
381 .ok_or_else(|| anyhow!("Could not find an available CID."))?;
382
383 system_properties::write(SYSPROP_LAST_CID, &format!("{}", cid))?;
384 Ok(cid)
385 }
386
387 fn find_available_cid<I>(&self, mut range: I) -> Option<Cid>
388 where
389 I: Iterator<Item = Cid>,
390 {
391 range.find(|cid| !self.held_contexts.contains_key(cid))
392 }
393
394 fn allocate_vm_context(
395 &mut self,
396 requester_uid: uid_t,
397 requester_debug_pid: pid_t,
398 ) -> Result<Strong<dyn IGlobalVmContext>> {
399 // Garbage collect unused VM contexts.
400 self.held_contexts.retain(|_, instance| instance.strong_count() > 0);
401
402 let cid = self.get_next_available_cid()?;
403 let instance = Arc::new(GlobalVmInstance { cid, requester_uid, requester_debug_pid });
David Brazdil2dfefd12023-11-17 14:07:36 +0000404 create_temporary_directory(&instance.get_temp_dir(), Some(requester_uid))?;
David Brazdilafc9a9e2023-01-12 16:08:10 +0000405
406 self.held_contexts.insert(cid, Arc::downgrade(&instance));
407 let binder = GlobalVmContext { instance, ..Default::default() };
408 Ok(BnGlobalVmContext::new_binder(binder, BinderFeatures::default()))
409 }
David Brazdil2dfefd12023-11-17 14:07:36 +0000410
411 fn get_dtbo_file(&mut self) -> Result<File> {
412 let mut file = self.dtbo_file.lock().unwrap();
413
414 let fd = if let Some(ref_fd) = &*file {
415 ref_fd.try_clone()?
416 } else {
417 let path = get_or_create_common_dir()?.join("vm.dtbo");
418 if path.exists() {
419 // All temporary files are deleted when the service is started.
420 // If the file exists but the FD is not cached, the file is
421 // likely corrupted.
422 remove_file(&path).context("Failed to clone cached VM DTBO file descriptor")?;
423 }
424
425 // Open a write-only file descriptor for vfio_handler.
426 let write_fd = File::create(&path).context("Failed to create VM DTBO file")?;
427
428 let vfio_service: Strong<dyn IVfioHandler> =
429 wait_for_interface(<BpVfioHandler as IVfioHandler>::get_descriptor())?;
430 vfio_service.writeVmDtbo(&ParcelFileDescriptor::new(write_fd))?;
431
432 // Open read-only. This FD will be cached and returned to clients.
433 let read_fd = File::open(&path).context("Failed to open VM DTBO file")?;
434 let read_fd_clone =
435 read_fd.try_clone().context("Failed to clone VM DTBO file descriptor")?;
436 *file = Some(read_fd);
437 read_fd_clone
438 };
439
440 Ok(fd)
441 }
David Brazdilafc9a9e2023-01-12 16:08:10 +0000442}
443
David Brazdil2dfefd12023-11-17 14:07:36 +0000444fn create_temporary_directory(path: &PathBuf, requester_uid: Option<uid_t>) -> Result<()> {
445 // Directory may exist if previous attempt to create it had failed.
446 // Delete it before trying again.
David Brazdilafc9a9e2023-01-12 16:08:10 +0000447 if path.as_path().exists() {
448 remove_temporary_dir(path).unwrap_or_else(|e| {
449 warn!("Could not delete temporary directory {:?}: {}", path, e);
450 });
451 }
David Brazdil2dfefd12023-11-17 14:07:36 +0000452 // Create directory.
453 create_dir(path).with_context(|| format!("Could not create temporary directory {:?}", path))?;
454 // If provided, change ownership to client's UID but system's GID, and permissions 0700.
David Brazdilafc9a9e2023-01-12 16:08:10 +0000455 // If the chown() fails, this will leave behind an empty directory that will get removed
456 // at the next attempt, or if virtualizationservice is restarted.
David Brazdil2dfefd12023-11-17 14:07:36 +0000457 if let Some(uid) = requester_uid {
458 chown(path, Some(Uid::from_raw(uid)), None).with_context(|| {
459 format!("Could not set ownership of temporary directory {:?}", path)
460 })?;
461 }
David Brazdilafc9a9e2023-01-12 16:08:10 +0000462 Ok(())
463}
464
465/// Removes a directory owned by a different user by first changing its owner back
466/// to VirtualizationService.
467pub fn remove_temporary_dir(path: &PathBuf) -> Result<()> {
Alice Wangd1b11a02023-04-18 12:30:20 +0000468 ensure!(path.as_path().is_dir(), "Path {:?} is not a directory", path);
David Brazdilafc9a9e2023-01-12 16:08:10 +0000469 chown(path, Some(Uid::current()), None)?;
470 set_permissions(path, Permissions::from_mode(0o700))?;
Alice Wangd1b11a02023-04-18 12:30:20 +0000471 remove_dir_all(path)?;
David Brazdilafc9a9e2023-01-12 16:08:10 +0000472 Ok(())
473}
474
David Brazdil2dfefd12023-11-17 14:07:36 +0000475fn get_or_create_common_dir() -> Result<PathBuf> {
476 let path = Path::new(TEMPORARY_DIRECTORY).join("common");
477 if !path.exists() {
478 create_temporary_directory(&path, None)?;
479 }
480 Ok(path)
481}
482
David Brazdilafc9a9e2023-01-12 16:08:10 +0000483/// Implementation of the AIDL `IGlobalVmContext` interface.
484#[derive(Debug, Default)]
485struct GlobalVmContext {
486 /// Strong reference to the context's instance data structure.
487 instance: Arc<GlobalVmInstance>,
488 /// Keeps our service process running as long as this VM context exists.
489 #[allow(dead_code)]
490 lazy_service_guard: LazyServiceGuard,
491}
492
493impl Interface for GlobalVmContext {}
494
495impl IGlobalVmContext for GlobalVmContext {
496 fn getCid(&self) -> binder::Result<i32> {
497 Ok(self.instance.cid as i32)
498 }
499
500 fn getTemporaryDirectory(&self) -> binder::Result<String> {
501 Ok(self.instance.get_temp_dir().to_string_lossy().to_string())
502 }
503}
504
505fn handle_stream_connection_tombstoned() -> Result<()> {
506 // Should not listen for tombstones on a guest VM's port.
507 assert!(!is_valid_guest_cid(VM_TOMBSTONES_SERVICE_PORT as Cid));
508 let listener =
509 VsockListener::bind_with_cid_port(VMADDR_CID_HOST, VM_TOMBSTONES_SERVICE_PORT as Cid)?;
510 for incoming_stream in listener.incoming() {
511 let mut incoming_stream = match incoming_stream {
512 Err(e) => {
513 warn!("invalid incoming connection: {:?}", e);
514 continue;
515 }
516 Ok(s) => s,
517 };
518 std::thread::spawn(move || {
519 if let Err(e) = handle_tombstone(&mut incoming_stream) {
520 error!("Failed to write tombstone- {:?}", e);
521 }
522 });
523 }
524 Ok(())
525}
526
527fn handle_tombstone(stream: &mut VsockStream) -> Result<()> {
528 if let Ok(addr) = stream.peer_addr() {
529 info!("Vsock Stream connected to cid={} for tombstones", addr.cid());
530 }
531 let tb_connection =
532 TombstonedConnection::connect(std::process::id() as i32, DebuggerdDumpType::Tombstone)
533 .context("Failed to connect to tombstoned")?;
534 let mut text_output = tb_connection
535 .text_output
536 .as_ref()
537 .ok_or_else(|| anyhow!("Could not get file to write the tombstones on"))?;
538 let mut num_bytes_read = 0;
539 loop {
540 let mut chunk_recv = [0; CHUNK_RECV_MAX_LEN];
541 let n = stream
542 .read(&mut chunk_recv)
543 .context("Failed to read tombstone data from Vsock stream")?;
544 if n == 0 {
545 break;
546 }
547 num_bytes_read += n;
548 text_output.write_all(&chunk_recv[0..n]).context("Failed to write guests tombstones")?;
549 }
550 info!("Received {} bytes from guest & wrote to tombstone file", num_bytes_read);
551 tb_connection.notify_completion()?;
552 Ok(())
553}
554
555/// Checks whether the caller has a specific permission
556fn check_permission(perm: &str) -> binder::Result<()> {
557 let calling_pid = get_calling_pid();
558 let calling_uid = get_calling_uid();
559 // Root can do anything
560 if calling_uid == 0 {
561 return Ok(());
562 }
563 let perm_svc: Strong<dyn IPermissionController::IPermissionController> =
564 binder::get_interface("permission")?;
565 if perm_svc.checkPermission(perm, calling_pid, calling_uid as i32)? {
566 Ok(())
567 } else {
Jiyong Park2227eaa2023-08-04 11:59:18 +0900568 Err(anyhow!("does not have the {} permission", perm))
569 .or_binder_exception(ExceptionCode::SECURITY)
David Brazdilafc9a9e2023-01-12 16:08:10 +0000570 }
571}
572
573/// Check whether the caller of the current Binder method is allowed to call debug methods.
574fn check_debug_access() -> binder::Result<()> {
575 check_permission("android.permission.DEBUG_VIRTUAL_MACHINE")
576}
577
578/// Check whether the caller of the current Binder method is allowed to manage VMs
579fn check_manage_access() -> binder::Result<()> {
580 check_permission("android.permission.MANAGE_VIRTUAL_MACHINE")
581}
Inseob Kim53d0b212023-07-20 16:58:37 +0900582
583/// Check whether the caller of the current Binder method is allowed to use custom VMs
584fn check_use_custom_virtual_machine() -> binder::Result<()> {
585 check_permission("android.permission.USE_CUSTOM_VIRTUAL_MACHINE")
586}
Alice Wang4c6c5582023-11-23 15:07:18 +0000587
588#[cfg(test)]
589mod tests {
590 use super::*;
591 use std::fs;
592
593 const TEST_RKP_CERT_CHAIN_PATH: &str = "testdata/rkp_cert_chain.der";
594
595 #[test]
596 fn splitting_x509_certificate_chain_succeeds() -> Result<()> {
597 let bytes = fs::read(TEST_RKP_CERT_CHAIN_PATH)?;
598 let cert_chain = split_x509_certificate_chain(&bytes)?;
599
600 assert_eq!(4, cert_chain.len());
601 for cert in cert_chain {
602 let (remaining, _) = X509Certificate::from_der(&cert.encodedCertificate)?;
603 assert!(remaining.is_empty());
604 }
605 Ok(())
606 }
607}