blob: 4555ebcf8b83537fd14f55091de154d28c43b3d5 [file] [log] [blame]
Andrew Walbrand6dce6f2021-03-05 16:39:08 +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
Andrew Walbranf6bf6862021-05-21 12:41:13 +000015//! Implementation of the AIDL interface of the VirtualizationService.
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000016
Seungjae Yoo13af0b62024-05-20 14:15:13 +090017use crate::{get_calling_pid, get_calling_uid, get_this_pid};
Alan Stokesfda70842023-12-20 17:50:14 +000018use crate::atom::{write_vm_booted_stats, write_vm_creation_stats};
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000019use crate::composite::make_composite_image;
Akilesh Kailash05515dc2024-10-22 21:42:52 -070020use crate::crosvm::{AudioConfig, CrosvmConfig, DiskFile, SharedPathConfig, DisplayConfig, GpuConfig, InputDeviceOption, PayloadState, UsbConfig, VmContext, VmInstance, VmState};
Jaewan Kim61f86142023-03-28 15:12:52 +090021use crate::debug_config::DebugConfig;
Shikha Panwar55e10ec2024-02-13 12:53:49 +000022use crate::dt_overlay::{create_device_tree_overlay, VM_DT_OVERLAY_MAX_SIZE, VM_DT_OVERLAY_PATH};
Nikita Ioffe5dfddf22023-06-29 16:11:26 +010023use crate::payload::{add_microdroid_payload_images, add_microdroid_system_images, add_microdroid_vendor_image};
Seungjae Yoofd9a0622022-10-14 10:01:29 +090024use crate::selinux::{getfilecon, SeContext};
Jiyong Park753553b2021-07-12 21:21:09 +090025use android_os_permissions_aidl::aidl::android::os::IPermissionController;
David Brazdil49f96f52022-12-16 21:29:13 +000026use android_system_virtualizationcommon::aidl::android::system::virtualizationcommon::{
Alice Wang4e3015d2023-10-10 09:35:37 +000027 Certificate::Certificate,
Andrew Walbranc92d35f2022-01-12 12:45:19 +000028 DeathReason::DeathReason,
David Brazdil49f96f52022-12-16 21:29:13 +000029 ErrorCode::ErrorCode,
30};
31use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{
Inseob Kim53d0b212023-07-20 16:58:37 +090032 AssignableDevice::AssignableDevice,
David Brazdil7d1e5ec2023-02-06 17:56:29 +000033 CpuTopology::CpuTopology,
Andrew Walbran6b650662021-09-07 13:13:23 +000034 DiskImage::DiskImage,
Akilesh Kailash05515dc2024-10-22 21:42:52 -070035 SharedPath::SharedPath,
Jeongik Chac181be72024-03-27 00:18:30 +090036 InputDevice::InputDevice,
Devin Moore407df8f2024-08-27 19:39:16 +000037 IVirtualMachine::{self, BnVirtualMachine},
Andrew Walbran6b650662021-09-07 13:13:23 +000038 IVirtualMachineCallback::IVirtualMachineCallback,
39 IVirtualizationService::IVirtualizationService,
Jiyong Park029977d2021-11-24 21:56:49 +090040 Partition::Partition,
Andrew Walbran6b650662021-09-07 13:13:23 +000041 PartitionType::PartitionType,
Inseob Kim0168b462022-12-27 14:54:35 +090042 VirtualMachineAppConfig::{DebugLevel::DebugLevel, Payload::Payload, VirtualMachineAppConfig},
Jooyung Han21e9b922021-06-26 04:14:16 +090043 VirtualMachineConfig::VirtualMachineConfig,
Andrew Walbran6b650662021-09-07 13:13:23 +000044 VirtualMachineDebugInfo::VirtualMachineDebugInfo,
Alan Stokes0d1ef782022-09-27 13:46:35 +010045 VirtualMachinePayloadConfig::VirtualMachinePayloadConfig,
Jooyung Han21e9b922021-06-26 04:14:16 +090046 VirtualMachineRawConfig::VirtualMachineRawConfig,
Andrew Walbran6b650662021-09-07 13:13:23 +000047 VirtualMachineState::VirtualMachineState,
Jooyung Han21e9b922021-06-26 04:14:16 +090048};
Inseob Kimecde8c02024-09-03 13:11:08 +090049use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::IGlobalVmContext::IGlobalVmContext;
David Brazdilafc9a9e2023-01-12 16:08:10 +000050use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::IVirtualizationServiceInternal::IVirtualizationServiceInternal;
Seungjae Yoodd91f0f2022-11-09 15:25:21 +090051use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::{
David Brazdilafc9a9e2023-01-12 16:08:10 +000052 BnVirtualMachineService, IVirtualMachineService,
Seungjae Yoofd9a0622022-10-14 10:01:29 +090053};
Shikha Panware45e9422024-02-28 21:18:10 +000054use android_hardware_security_secretkeeper::aidl::android::hardware::security::secretkeeper::ISecretkeeper::{BnSecretkeeper, ISecretkeeper};
Shikha Panwar5d6a6752023-12-14 22:08:26 +000055use android_hardware_security_secretkeeper::aidl::android::hardware::security::secretkeeper::SecretId::SecretId;
56use android_hardware_security_authgraph::aidl::android::hardware::security::authgraph::{
57 Arc::Arc as AuthgraphArc, IAuthGraphKeyExchange::IAuthGraphKeyExchange,
58 IAuthGraphKeyExchange::BnAuthGraphKeyExchange, Identity::Identity, KeInitResult::KeInitResult,
59 Key::Key, PubKey::PubKey, SessionIdSignature::SessionIdSignature, SessionInfo::SessionInfo,
60 SessionInitiationInfo::SessionInitiationInfo,
61};
Alan Stokes25f69362023-03-06 16:51:54 +000062use anyhow::{anyhow, bail, Context, Result};
Seungjae Yoofd9a0622022-10-14 10:01:29 +090063use apkverify::{HashAlgorithm, V4Signature};
Jiyong Parkd7bd2f22023-08-10 20:41:19 +090064use avflog::LogResult;
Alan Stokes0e82b502022-08-08 14:44:48 +010065use binder::{
Devin Moore407df8f2024-08-27 19:39:16 +000066 self, wait_for_interface, Accessor, BinderFeatures, ConnectionInfo, ExceptionCode, Interface, ParcelFileDescriptor,
67 SpIBinder, Status, StatusCode, Strong, IntoBinderResult,
Andrew Walbrana89fc132021-03-17 17:08:36 +000068};
Shikha Panwar55e10ec2024-02-13 12:53:49 +000069use cstr::cstr;
Inseob Kim46257382024-01-03 15:41:22 +090070use glob::glob;
Devin Moorec8800a12024-10-17 17:56:18 +000071use libc::{AF_VSOCK, sa_family_t, sockaddr_vm};
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +000072use log::{debug, error, info, warn};
Inseob Kim89b24592024-02-23 18:59:43 +090073use microdroid_payload_config::{ApkConfig, Task, TaskType, VmPayloadConfig};
Inseob Kim0168b462022-12-27 14:54:35 +090074use nix::unistd::pipe;
David Brazdil73988ea2022-11-11 15:10:32 +000075use rpcbinder::RpcServer;
Alan Stokes25f69362023-03-06 16:51:54 +000076use rustutils::system_properties;
Jiyong Parkdcf17412022-02-08 15:07:23 +090077use semver::VersionReq;
Inseob Kimecde8c02024-09-03 13:11:08 +090078use serde::Deserialize;
Inseob Kim6ef80972023-07-20 17:23:36 +090079use std::collections::HashSet;
Andrew Walbrandff3b942021-06-09 15:20:36 +000080use std::convert::TryInto;
Shikha Panwar55e10ec2024-02-13 12:53:49 +000081use std::fs;
Jaewan Kim39952072024-01-19 17:04:53 +090082use std::ffi::CStr;
Inseob Kimecde8c02024-09-03 13:11:08 +090083use std::fs::{canonicalize, create_dir_all, read_dir, remove_dir_all, remove_file, File, OpenOptions};
Shikha Panwar9ae2e622024-01-30 12:22:30 +000084use std::io::{BufRead, BufReader, Error, ErrorKind, Seek, SeekFrom, Write};
Inseob Kim46257382024-01-03 15:41:22 +090085use std::iter;
Nikita Ioffe5776f082023-02-10 21:38:26 +000086use std::num::{NonZeroU16, NonZeroU32};
Inseob Kimecde8c02024-09-03 13:11:08 +090087use std::ops::Range;
Jiyong Parkaf63d1c2024-09-04 16:15:42 +090088use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
David Brazdilafc9a9e2023-01-12 16:08:10 +000089use std::os::unix::raw::pid_t;
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000090use std::path::{Path, PathBuf};
Andrew Walbran9c03a3a2024-09-03 12:12:59 +010091use std::sync::{Arc, Mutex, Weak, LazyLock};
Seungjae Yoo14e60182024-02-21 13:28:31 +090092use vbmeta::VbMetaImage;
Pierre-Clément Tosid3bbe1d2024-04-15 18:03:51 +010093use vmconfig::{VmConfig, get_debug_level};
Devin Moorec8800a12024-10-17 17:56:18 +000094use vsock::VsockStream;
Jooyung Han35edb8f2021-07-01 16:17:16 +090095use zip::ZipArchive;
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000096
David Brazdil41d1a872022-10-05 14:44:19 +010097/// The unique ID of a VM used (together with a port number) for vsock communication.
98pub type Cid = u32;
99
David Brazdil4b4c5102022-12-19 22:56:20 +0000100pub const BINDER_SERVICE_IDENTIFIER: &str = "android.system.virtualizationservice";
101
Devin Moore407df8f2024-08-27 19:39:16 +0000102/// Vsock privileged ports are below this number.
103const VSOCK_PRIV_PORT_MAX: u32 = 1024;
104
Jooyung Han95884632021-07-06 22:27:54 +0900105/// The size of zero.img.
106/// Gaps in composite disk images are filled with a shared zero.img.
107const ZERO_FILLER_SIZE: u64 = 4096;
108
David Brazdilf50c7a62023-04-19 14:22:42 +0000109/// Magic string for the instance image
110const ANDROID_VM_INSTANCE_MAGIC: &str = "Android-VM-instance";
111
112/// Version of the instance image format
113const ANDROID_VM_INSTANCE_VERSION: u16 = 1;
114
Alan Stokes0d1ef782022-09-27 13:46:35 +0100115const MICRODROID_OS_NAME: &str = "microdroid";
116
Shikha Panwar5d6a6752023-12-14 22:08:26 +0000117const SECRETKEEPER_IDENTIFIER: &str =
Shikha Panwarbe5dee72024-02-21 19:06:20 +0000118 "android.hardware.security.secretkeeper.ISecretkeeper/default";
Shikha Panwar5d6a6752023-12-14 22:08:26 +0000119
David Brazdilf50c7a62023-04-19 14:22:42 +0000120const UNFORMATTED_STORAGE_MAGIC: &str = "UNFORMATTED-STORAGE";
121
122/// crosvm requires all partitions to be a multiple of 4KiB.
123const PARTITION_GRANULARITY_BYTES: u64 = 4096;
124
Shikha Panwar55e10ec2024-02-13 12:53:49 +0000125const VM_REFERENCE_DT_ON_HOST_PATH: &str = "/proc/device-tree/avf/reference";
126
Andrew Walbran9c03a3a2024-09-03 12:12:59 +0100127pub static GLOBAL_SERVICE: LazyLock<Strong<dyn IVirtualizationServiceInternal>> =
128 LazyLock::new(|| {
Inseob Kimecde8c02024-09-03 13:11:08 +0900129 if cfg!(early) {
130 panic!("Early virtmgr must not connect to VirtualizatinoServiceInternal")
131 } else {
132 wait_for_interface(BINDER_SERVICE_IDENTIFIER)
133 .expect("Could not connect to VirtualizationServiceInternal")
134 }
Andrew Walbran9c03a3a2024-09-03 12:12:59 +0100135 });
136static SUPPORTED_OS_NAMES: LazyLock<HashSet<String>> =
137 LazyLock::new(|| get_supported_os_names().expect("Failed to get list of supported os names"));
David Brazdil49f96f52022-12-16 21:29:13 +0000138
Nikita Ioffef1ce9872022-12-09 13:31:59 +0000139fn create_or_update_idsig_file(
140 input_fd: &ParcelFileDescriptor,
141 idsig_fd: &ParcelFileDescriptor,
142) -> Result<()> {
143 let mut input = clone_file(input_fd)?;
144 let metadata = input.metadata().context("failed to get input metadata")?;
145 if !metadata.is_file() {
146 bail!("input is not a regular file");
147 }
Alan Stokes25f69362023-03-06 16:51:54 +0000148 let mut sig =
149 V4Signature::create(&mut input, get_current_sdk()?, 4096, &[], HashAlgorithm::SHA256)
150 .context("failed to create idsig")?;
Nikita Ioffef1ce9872022-12-09 13:31:59 +0000151
152 let mut output = clone_file(idsig_fd)?;
Jiyong Park8d192952023-06-26 14:29:51 +0900153
154 // Optimization. We don't have to update idsig file whenever a VM is started. Don't update it,
155 // if the idsig file already has the same APK digest.
156 if output.metadata()?.len() > 0 {
157 if let Ok(out_sig) = V4Signature::from_idsig(&mut output) {
158 if out_sig.signing_info.apk_digest == sig.signing_info.apk_digest {
159 debug!("idsig {:?} is up-to-date with apk {:?}.", output, input);
160 return Ok(());
161 }
162 }
163 // if we fail to read v4signature from output, that's fine. User can pass a random file.
164 // We will anyway overwrite the file to the v4signature generated from input_fd.
165 }
166
Shikha Panwar9ae2e622024-01-30 12:22:30 +0000167 output
168 .seek(SeekFrom::Start(0))
169 .context("failed to move cursor to start on the idsig output")?;
Nikita Ioffec09b0492022-12-14 20:18:33 +0000170 output.set_len(0).context("failed to set_len on the idsig output")?;
171 sig.write_into(&mut output).context("failed to write idsig")?;
Nikita Ioffef1ce9872022-12-09 13:31:59 +0000172 Ok(())
173}
174
Alan Stokes25f69362023-03-06 16:51:54 +0000175fn get_current_sdk() -> Result<u32> {
176 let current_sdk = system_properties::read("ro.build.version.sdk")?;
177 let current_sdk = current_sdk.ok_or_else(|| anyhow!("SDK version missing"))?;
178 current_sdk.parse().context("Malformed SDK version")
179}
180
David Brazdil4b4c5102022-12-19 22:56:20 +0000181pub fn remove_temporary_files(path: &PathBuf) -> Result<()> {
182 for dir_entry in read_dir(path)? {
183 remove_file(dir_entry?.path())?;
184 }
185 Ok(())
David Brazdil528e0472022-10-10 15:06:02 +0100186}
187
David Brazdil528e0472022-10-10 15:06:02 +0100188/// Implementation of `IVirtualizationService`, the entry point of the AIDL service.
David Brazdil49f96f52022-12-16 21:29:13 +0000189#[derive(Debug, Default)]
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000190pub struct VirtualizationService {
Jiyong Park8611a6c2021-07-09 18:17:44 +0900191 state: Arc<Mutex<State>>,
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000192}
193
Shikha Panward8e35422021-10-11 13:51:27 +0000194impl Interface for VirtualizationService {
Andrei Homescu0cf8e222023-11-09 04:27:55 +0000195 fn dump(&self, writer: &mut dyn Write, _args: &[&CStr]) -> Result<(), StatusCode> {
Shikha Panward8e35422021-10-11 13:51:27 +0000196 check_permission("android.permission.DUMP").or(Err(StatusCode::PERMISSION_DENIED))?;
197 let state = &mut *self.state.lock().unwrap();
198 let vms = state.vms();
Andrei Homescu0cf8e222023-11-09 04:27:55 +0000199 writeln!(writer, "Running {0} VMs:", vms.len()).or(Err(StatusCode::UNKNOWN_ERROR))?;
Shikha Panward8e35422021-10-11 13:51:27 +0000200 for vm in vms {
Andrei Homescu0cf8e222023-11-09 04:27:55 +0000201 writeln!(writer, "VM CID: {}", vm.cid).or(Err(StatusCode::UNKNOWN_ERROR))?;
202 writeln!(writer, "\tState: {:?}", vm.vm_state.lock().unwrap())
Shikha Panward8e35422021-10-11 13:51:27 +0000203 .or(Err(StatusCode::UNKNOWN_ERROR))?;
Andrei Homescu0cf8e222023-11-09 04:27:55 +0000204 writeln!(writer, "\tPayload state {:?}", vm.payload_state())
Shikha Panward8e35422021-10-11 13:51:27 +0000205 .or(Err(StatusCode::UNKNOWN_ERROR))?;
Andrei Homescu0cf8e222023-11-09 04:27:55 +0000206 writeln!(writer, "\tProtected: {}", vm.protected).or(Err(StatusCode::UNKNOWN_ERROR))?;
207 writeln!(writer, "\ttemporary_directory: {}", vm.temporary_directory.to_string_lossy())
Shikha Panward8e35422021-10-11 13:51:27 +0000208 .or(Err(StatusCode::UNKNOWN_ERROR))?;
Andrei Homescu0cf8e222023-11-09 04:27:55 +0000209 writeln!(writer, "\trequester_uid: {}", vm.requester_uid)
Shikha Panward8e35422021-10-11 13:51:27 +0000210 .or(Err(StatusCode::UNKNOWN_ERROR))?;
Andrei Homescu0cf8e222023-11-09 04:27:55 +0000211 writeln!(writer, "\trequester_debug_pid: {}", vm.requester_debug_pid)
Shikha Panward8e35422021-10-11 13:51:27 +0000212 .or(Err(StatusCode::UNKNOWN_ERROR))?;
213 }
214 Ok(())
215 }
216}
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000217impl IVirtualizationService for VirtualizationService {
Andrew Walbranf8d94112021-09-07 11:45:36 +0000218 /// Creates (but does not start) a new VM with the given configuration, assigning it the next
219 /// available CID.
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000220 ///
221 /// Returns a binder `IVirtualMachine` object referring to it, as a handle for the client.
Andrew Walbranf8d94112021-09-07 11:45:36 +0000222 fn createVm(
Andrew Walbrana89fc132021-03-17 17:08:36 +0000223 &self,
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000224 config: &VirtualMachineConfig,
Jiyong Parke6fb1672023-06-26 16:45:55 +0900225 console_out_fd: Option<&ParcelFileDescriptor>,
226 console_in_fd: Option<&ParcelFileDescriptor>,
Andrew Walbrana89fc132021-03-17 17:08:36 +0000227 log_fd: Option<&ParcelFileDescriptor>,
Elie Kheirallah5c807a22024-09-23 20:40:42 +0000228 dump_dt_fd: Option<&ParcelFileDescriptor>,
Devin Moore407df8f2024-08-27 19:39:16 +0000229 ) -> binder::Result<Strong<dyn IVirtualMachine::IVirtualMachine>> {
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000230 let mut is_protected = false;
Jiyong Parke6fb1672023-06-26 16:45:55 +0900231 let ret = self.create_vm_internal(
232 config,
233 console_out_fd,
234 console_in_fd,
235 log_fd,
236 &mut is_protected,
Elie Kheirallah5c807a22024-09-23 20:40:42 +0000237 dump_dt_fd,
Jiyong Parke6fb1672023-06-26 16:45:55 +0900238 );
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +0000239 write_vm_creation_stats(config, is_protected, &ret);
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000240 ret
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000241 }
Andrew Walbran320b5602021-03-04 16:11:12 +0000242
Shikha Panwar61a74b52024-02-16 13:17:01 +0000243 /// Allocate a new instance_id to the VM
244 fn allocateInstanceId(&self) -> binder::Result<[u8; 64]> {
Shikha Panwarbd5eba92024-03-27 20:23:31 +0000245 check_manage_access()?;
Shikha Panwar61a74b52024-02-16 13:17:01 +0000246 GLOBAL_SERVICE.allocateInstanceId()
247 }
248
Andrew Walbrandff3b942021-06-09 15:20:36 +0000249 /// Initialise an empty partition image of the given size to be used as a writable partition.
250 fn initializeWritablePartition(
251 &self,
252 image_fd: &ParcelFileDescriptor,
Alan Stokesff0005f2023-01-30 09:53:00 +0000253 size_bytes: i64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900254 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000255 ) -> binder::Result<()> {
Jiyong Park753553b2021-07-12 21:21:09 +0900256 check_manage_access()?;
Jiyong Park2227eaa2023-08-04 11:59:18 +0900257 let size_bytes = size_bytes
258 .try_into()
259 .with_context(|| format!("Invalid size: {}", size_bytes))
260 .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT)?;
David Brazdilf50c7a62023-04-19 14:22:42 +0000261 let size_bytes = round_up(size_bytes, PARTITION_GRANULARITY_BYTES);
Shikha Panwar9ae2e622024-01-30 12:22:30 +0000262 let mut image = clone_file(image_fd)?;
David Brazdilf50c7a62023-04-19 14:22:42 +0000263 // initialize the file. Any data in the file will be erased.
Shikha Panwar9ae2e622024-01-30 12:22:30 +0000264 image
265 .seek(SeekFrom::Start(0))
266 .context("failed to move cursor to start")
267 .or_service_specific_exception(-1)?;
Jiyong Park2227eaa2023-08-04 11:59:18 +0900268 image.set_len(0).context("Failed to reset a file").or_service_specific_exception(-1)?;
Frederick Mayle0d310572024-05-02 12:34:58 -0700269 // Set the file length. In most filesystems, this will not allocate any physical disk
270 // space, it will only change the logical size.
271 image
272 .set_len(size_bytes)
273 .context("Failed to extend file")
Jiyong Park2227eaa2023-08-04 11:59:18 +0900274 .or_service_specific_exception(-1)?;
David Brazdilf50c7a62023-04-19 14:22:42 +0000275
276 match partition_type {
277 PartitionType::RAW => Ok(()),
Frederick Mayle0d310572024-05-02 12:34:58 -0700278 PartitionType::ANDROID_VM_INSTANCE => format_as_android_vm_instance(&mut image),
279 PartitionType::ENCRYPTEDSTORE => format_as_encryptedstore(&mut image),
David Brazdilf50c7a62023-04-19 14:22:42 +0000280 _ => Err(Error::new(
281 ErrorKind::Unsupported,
282 format!("Unsupported partition type {:?}", partition_type),
283 )),
284 }
Jiyong Park2227eaa2023-08-04 11:59:18 +0900285 .with_context(|| format!("Failed to initialize partition as {:?}", partition_type))
286 .or_service_specific_exception(-1)?;
David Brazdilf50c7a62023-04-19 14:22:42 +0000287
288 Ok(())
Andrew Walbrandff3b942021-06-09 15:20:36 +0000289 }
290
Jiyong Park0a248432021-08-20 23:32:39 +0900291 /// Creates or update the idsig file by digesting the input APK file.
292 fn createOrUpdateIdsigFile(
293 &self,
294 input_fd: &ParcelFileDescriptor,
295 idsig_fd: &ParcelFileDescriptor,
296 ) -> binder::Result<()> {
Jiyong Parkc3ca24f2022-06-28 10:45:15 +0900297 check_manage_access()?;
298
Jiyong Park2227eaa2023-08-04 11:59:18 +0900299 create_or_update_idsig_file(input_fd, idsig_fd).or_service_specific_exception(-1)?;
Jiyong Park0a248432021-08-20 23:32:39 +0900300 Ok(())
301 }
302
Andrew Walbran320b5602021-03-04 16:11:12 +0000303 /// Get a list of all currently running VMs. This method is only intended for debug purposes,
304 /// and as such is only permitted from the shell user.
305 fn debugListVms(&self) -> binder::Result<Vec<VirtualMachineDebugInfo>> {
David Brazdil209074a2023-01-12 16:44:51 +0000306 // Delegate to the global service, including checking the debug permission.
David Brazdild4f51a52023-01-11 14:09:27 +0000307 GLOBAL_SERVICE.debugListVms()
Andrew Walbran320b5602021-03-04 16:11:12 +0000308 }
Inseob Kim53d0b212023-07-20 16:58:37 +0900309
310 /// Get a list of assignable device types.
311 fn getAssignableDevices(&self) -> binder::Result<Vec<AssignableDevice>> {
312 // Delegate to the global service, including checking the permission.
313 GLOBAL_SERVICE.getAssignableDevices()
314 }
Nikita Ioffef7c742a2023-09-04 16:57:59 +0100315
Inseob Kim46257382024-01-03 15:41:22 +0900316 /// Get a list of supported OSes.
317 fn getSupportedOSList(&self) -> binder::Result<Vec<String>> {
318 Ok(Vec::from_iter(SUPPORTED_OS_NAMES.iter().cloned()))
319 }
320
Nikita Ioffef7c742a2023-09-04 16:57:59 +0100321 /// Returns whether given feature is enabled
322 fn isFeatureEnabled(&self, feature: &str) -> binder::Result<bool> {
323 check_manage_access()?;
Shikha Panwar6d306412024-02-17 21:37:49 +0000324 Ok(avf_features::is_feature_enabled(feature))
Nikita Ioffef7c742a2023-09-04 16:57:59 +0100325 }
Alice Wange64dd182024-01-17 15:57:55 +0000326
327 fn enableTestAttestation(&self) -> binder::Result<()> {
328 GLOBAL_SERVICE.enableTestAttestation()
329 }
Alice Wang0362f7f2024-03-21 08:16:26 +0000330
331 fn isRemoteAttestationSupported(&self) -> binder::Result<bool> {
332 check_manage_access()?;
333 GLOBAL_SERVICE.isRemoteAttestationSupported()
334 }
Shikha Panwar7aef66d2024-03-13 00:28:41 +0000335
336 fn isUpdatableVmSupported(&self) -> binder::Result<bool> {
337 // The response is specific to Microdroid. Updatable VMs are only possible if device
338 // supports Secretkeeper. Guest OS needs to use Secretkeeper based secrets. Microdroid does
339 // this, however other guest OSes may do things differently.
340 check_manage_access()?;
341 Ok(is_secretkeeper_supported())
342 }
Shikha Panwarbd5eba92024-03-27 20:23:31 +0000343
Shikha Panwar07d701c2024-03-28 14:43:52 +0000344 fn removeVmInstance(&self, instance_id: &[u8; 64]) -> binder::Result<()> {
345 check_manage_access()?;
346 GLOBAL_SERVICE.removeVmInstance(instance_id)
347 }
348
Shikha Panwarbd5eba92024-03-27 20:23:31 +0000349 fn claimVmInstance(&self, instance_id: &[u8; 64]) -> binder::Result<()> {
350 check_manage_access()?;
351 GLOBAL_SERVICE.claimVmInstance(instance_id)
352 }
Andrew Walbran320b5602021-03-04 16:11:12 +0000353}
354
Inseob Kimecde8c02024-09-03 13:11:08 +0900355/// Implementation of the AIDL `IGlobalVmContext` interface for early VMs.
356#[derive(Debug, Default)]
357struct EarlyVmContext {
358 /// The unique CID assigned to the VM for vsock communication.
359 cid: Cid,
360 /// Temporary directory for this VM instance.
361 temp_dir: PathBuf,
362}
363
364impl EarlyVmContext {
365 fn new(cid: Cid, temp_dir: PathBuf) -> Result<Self> {
366 // Remove the entire directory before creating a VM. Early VMs use predefined CIDs and AVF
367 // should trust clients, e.g. they won't run two VMs at the same time
368 let _ = remove_dir_all(&temp_dir);
369 create_dir_all(&temp_dir).context(format!("can't create '{}'", temp_dir.display()))?;
370
371 Ok(Self { cid, temp_dir })
372 }
373}
374
375impl Interface for EarlyVmContext {}
376
377impl Drop for EarlyVmContext {
378 fn drop(&mut self) {
379 if let Err(e) = remove_dir_all(&self.temp_dir) {
380 error!("Cannot remove {} upon dropping: {e}", self.temp_dir.display());
381 }
382 }
383}
384
385impl IGlobalVmContext for EarlyVmContext {
386 fn getCid(&self) -> binder::Result<i32> {
387 Ok(self.cid as i32)
388 }
389
390 fn getTemporaryDirectory(&self) -> binder::Result<String> {
391 Ok(self.temp_dir.to_string_lossy().to_string())
392 }
393
394 fn setHostConsoleName(&self, _pathname: &str) -> binder::Result<()> {
395 Err(Status::new_exception_str(
396 ExceptionCode::UNSUPPORTED_OPERATION,
397 Some("Early VM doesn't support setting host console name"),
398 ))
399 }
400}
401
402fn find_partition(path: &Path) -> binder::Result<String> {
403 match path.components().nth(1) {
404 Some(std::path::Component::Normal(partition)) => {
405 Ok(partition.to_string_lossy().into_owned())
406 }
407 _ => Err(anyhow!("Can't find partition in '{}'", path.display()))
408 .or_service_specific_exception(-1),
409 }
410}
411
Jiyong Park8611a6c2021-07-09 18:17:44 +0900412impl VirtualizationService {
413 pub fn init() -> VirtualizationService {
David Brazdil49f96f52022-12-16 21:29:13 +0000414 VirtualizationService::default()
Jiyong Park8611a6c2021-07-09 18:17:44 +0900415 }
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000416
Inseob Kimecde8c02024-09-03 13:11:08 +0900417 fn create_early_vm_context(
418 &self,
419 config: &VirtualMachineConfig,
420 ) -> binder::Result<(VmContext, Cid, PathBuf)> {
421 let calling_exe_path = format!("/proc/{}/exe", get_calling_pid());
422 let link = fs::read_link(&calling_exe_path)
423 .context(format!("can't read_link '{calling_exe_path}'"))
424 .or_service_specific_exception(-1)?;
425 let partition = find_partition(&link)?;
426
427 let name = match config {
428 VirtualMachineConfig::RawConfig(config) => &config.name,
429 VirtualMachineConfig::AppConfig(config) => &config.name,
430 };
431 let early_vm =
432 find_early_vm_for_partition(&partition, name).or_service_specific_exception(-1)?;
433 if Path::new(&early_vm.path) != link {
434 return Err(anyhow!(
435 "VM '{name}' in partition '{partition}' must be created with '{}', not '{}'",
436 &early_vm.path,
437 link.display()
438 ))
439 .or_service_specific_exception(-1);
440 }
441
442 let cid = early_vm.cid as Cid;
443 let temp_dir = PathBuf::from(format!("/mnt/vm/early/{cid}"));
444
445 let context = EarlyVmContext::new(cid, temp_dir.clone())
446 .context(format!("Can't create early vm contexts for {cid}"))
447 .or_service_specific_exception(-1)?;
448 let service = VirtualMachineService::new_binder(self.state.clone(), cid).as_binder();
449
450 // Start VM service listening for connections from the new CID on port=CID.
451 let port = cid;
Devin Moore068e6742024-10-28 18:16:25 +0000452 let (vm_server, _) = RpcServer::new_vsock(service, cid, port)
Inseob Kimecde8c02024-09-03 13:11:08 +0900453 .context(format!("Could not start RpcServer on port {port}"))
454 .or_service_specific_exception(-1)?;
455 vm_server.start();
456 Ok((VmContext::new(Strong::new(Box::new(context)), vm_server), cid, temp_dir))
457 }
458
David Brazdil209074a2023-01-12 16:44:51 +0000459 fn create_vm_context(
460 &self,
461 requester_debug_pid: pid_t,
462 ) -> binder::Result<(VmContext, Cid, PathBuf)> {
David Brazdil8cf8f482022-11-23 14:21:26 +0000463 const NUM_ATTEMPTS: usize = 5;
464
465 for _ in 0..NUM_ATTEMPTS {
Charisee96113f32023-01-26 09:00:42 +0000466 let vm_context = GLOBAL_SERVICE.allocateGlobalVmContext(requester_debug_pid)?;
David Brazdild4f51a52023-01-11 14:09:27 +0000467 let cid = vm_context.getCid()? as Cid;
468 let temp_dir: PathBuf = vm_context.getTemporaryDirectory()?.into();
David Brazdil8cf8f482022-11-23 14:21:26 +0000469 let service = VirtualMachineService::new_binder(self.state.clone(), cid).as_binder();
470
471 // Start VM service listening for connections from the new CID on port=CID.
David Brazdil8cf8f482022-11-23 14:21:26 +0000472 let port = cid;
David Brazdil3238da42022-11-18 10:04:51 +0000473 match RpcServer::new_vsock(service, cid, port) {
Devin Moore068e6742024-10-28 18:16:25 +0000474 Ok((vm_server, _)) => {
David Brazdil8cf8f482022-11-23 14:21:26 +0000475 vm_server.start();
David Brazdild4f51a52023-01-11 14:09:27 +0000476 return Ok((VmContext::new(vm_context, vm_server), cid, temp_dir));
David Brazdil8cf8f482022-11-23 14:21:26 +0000477 }
478 Err(err) => {
479 warn!("Could not start RpcServer on port {}: {}", port, err);
480 }
481 }
482 }
Jiyong Park2227eaa2023-08-04 11:59:18 +0900483 Err(anyhow!("Too many attempts to create VM context failed"))
484 .or_service_specific_exception(-1)
David Brazdil8cf8f482022-11-23 14:21:26 +0000485 }
486
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000487 fn create_vm_internal(
488 &self,
489 config: &VirtualMachineConfig,
Jiyong Parke6fb1672023-06-26 16:45:55 +0900490 console_out_fd: Option<&ParcelFileDescriptor>,
491 console_in_fd: Option<&ParcelFileDescriptor>,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000492 log_fd: Option<&ParcelFileDescriptor>,
493 is_protected: &mut bool,
Elie Kheirallah5c807a22024-09-23 20:40:42 +0000494 dump_dt_fd: Option<&ParcelFileDescriptor>,
Devin Moore407df8f2024-08-27 19:39:16 +0000495 ) -> binder::Result<Strong<dyn IVirtualMachine::IVirtualMachine>> {
David Brazdil209074a2023-01-12 16:44:51 +0000496 let requester_uid = get_calling_uid();
497 let requester_debug_pid = get_calling_pid();
498
Nikita Ioffe631717e2023-09-05 13:38:07 +0100499 check_config_features(config)?;
500
Inseob Kimecde8c02024-09-03 13:11:08 +0900501 if cfg!(early) {
502 check_config_allowed_for_early_vms(config)?;
503 }
504
David Brazdil209074a2023-01-12 16:44:51 +0000505 // Allocating VM context checks the MANAGE_VIRTUAL_MACHINE permission.
Inseob Kimecde8c02024-09-03 13:11:08 +0900506 let (vm_context, cid, temporary_directory) = if cfg!(early) {
507 self.create_early_vm_context(config)?
508 } else {
509 self.create_vm_context(requester_debug_pid)?
510 };
Inseob Kim1119d702022-05-02 18:01:58 +0900511
Alan Stokesfda70842023-12-20 17:50:14 +0000512 if is_custom_config(config) {
Alan Stokes7bc146c2022-10-20 17:10:32 +0100513 check_use_custom_virtual_machine()?;
Inseob Kim1119d702022-05-02 18:01:58 +0900514 }
515
Nikita Ioffe5776f082023-02-10 21:38:26 +0000516 let gdb_port = extract_gdb_port(config);
517
518 // Additional permission checks if caller request gdb.
519 if gdb_port.is_some() {
520 check_gdb_allowed(config)?;
521 }
522
Nikita Ioffeb2c6f3a2024-06-06 12:51:53 +0000523 let device_tree_overlay = maybe_create_device_tree_overlay(config, &temporary_directory)?;
Seungjae Yooec3bc522023-11-09 10:14:30 +0900524
Jaewan Kimf3143242024-03-15 06:56:31 +0000525 let debug_config = DebugConfig::new(config);
Nikita Ioffe2cb75cf2024-06-04 16:43:56 +0000526 let ramdump = if !uses_gki_kernel(config) && debug_config.is_ramdump_needed() {
Jiyong Parked180932023-02-24 19:55:41 +0900527 Some(prepare_ramdump_file(&temporary_directory)?)
528 } else {
529 None
530 };
531
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000532 let state = &mut *self.state.lock().unwrap();
Jiyong Parke6fb1672023-06-26 16:45:55 +0900533 let console_out_fd =
Pierre-Clément Tosifc523c52024-08-20 10:34:06 +0100534 clone_or_prepare_logger_fd(console_out_fd, format!("Console({})", cid))?;
Jiyong Parke6fb1672023-06-26 16:45:55 +0900535 let console_in_fd = console_in_fd.map(clone_file).transpose()?;
Pierre-Clément Tosifc523c52024-08-20 10:34:06 +0100536 let log_fd = clone_or_prepare_logger_fd(log_fd, format!("Log({})", cid))?;
Pierre-Clément Tosi9515d0f2024-04-15 15:58:07 +0100537 let dump_dt_fd = if let Some(fd) = dump_dt_fd {
538 Some(clone_file(fd)?)
539 } else if debug_config.dump_device_tree {
540 Some(prepare_dump_dt_file(&temporary_directory)?)
541 } else {
542 None
543 };
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000544
545 // Counter to generate unique IDs for temporary image files.
546 let mut next_temporary_image_id = 0;
547 // Files which are referred to from composite images. These must be mapped to the crosvm
548 // child process, and not closed before it is started.
549 let mut indirect_files = vec![];
550
Alan Stokes7bc146c2022-10-20 17:10:32 +0100551 let (is_app_config, config) = match config {
552 VirtualMachineConfig::RawConfig(config) => (false, BorrowedOrOwned::Borrowed(config)),
553 VirtualMachineConfig::AppConfig(config) => {
Jiyong Park2227eaa2023-08-04 11:59:18 +0900554 let config = load_app_config(config, &debug_config, &temporary_directory)
555 .or_service_specific_exception_with(-1, |e| {
Jaewan Kim61f86142023-03-28 15:12:52 +0900556 *is_protected = config.protectedVm;
557 let message = format!("Failed to load app config: {:?}", e);
558 error!("{}", message);
Jiyong Park2227eaa2023-08-04 11:59:18 +0900559 message
Jaewan Kim61f86142023-03-28 15:12:52 +0900560 })?;
Alan Stokes7bc146c2022-10-20 17:10:32 +0100561 (true, BorrowedOrOwned::Owned(config))
562 }
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000563 };
564 let config = config.as_ref();
565 *is_protected = config.protectedVm;
566
567 // Check if partition images are labeled incorrectly. This is to prevent random images
568 // which are not protected by the Android Verified Boot (e.g. bits downloaded by apps) from
Alan Stokes3e5eec12023-09-07 12:10:00 +0100569 // being loaded in a pVM. This applies to everything but the instance image in the raw
570 // config, and everything but the non-executable, generated partitions in the app
571 // config.
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000572 config
573 .disks
574 .iter()
575 .flat_map(|disk| disk.partitions.iter())
576 .filter(|partition| {
577 if is_app_config {
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100578 !is_safe_app_partition(&partition.label)
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000579 } else {
Alice Wangc206b9b2023-08-28 14:13:51 +0000580 !is_safe_raw_partition(&partition.label)
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000581 }
582 })
583 .try_for_each(check_label_for_partition)
Jiyong Park2227eaa2023-08-04 11:59:18 +0900584 .or_service_specific_exception(-1)?;
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000585
Inseob Kimff48a7c2024-02-26 22:07:21 +0900586 // Check if files for payloads and bases are NOT coming from /vendor and /odm, as they may
587 // have unstable interfaces.
588 // TODO(b/316431494): remove once Treble interfaces are stabilized.
589 check_partitions_for_files(config).or_service_specific_exception(-1)?;
590
Alan Stokes185fe112023-01-10 16:20:55 +0000591 let kernel = maybe_clone_file(&config.kernel)?;
592 let initrd = maybe_clone_file(&config.initrd)?;
593
Alan Stokes185fe112023-01-10 16:20:55 +0000594 if config.protectedVm {
Nikita Ioffef934e812024-07-05 15:44:41 +0000595 // In a protected VM, we require custom kernels to come from a trusted source
596 // (b/237054515).
Jiyong Park2227eaa2023-08-04 11:59:18 +0900597 check_label_for_kernel_files(&kernel, &initrd).or_service_specific_exception(-1)?;
Nikita Ioffef934e812024-07-05 15:44:41 +0000598 // Fail fast with a meaningful error message in case device doesn't support pVMs.
599 check_protected_vm_is_supported()?;
Alan Stokes185fe112023-01-10 16:20:55 +0000600 }
601
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000602 let zero_filler_path = temporary_directory.join("zero.img");
Jiyong Park2227eaa2023-08-04 11:59:18 +0900603 write_zero_filler(&zero_filler_path)
604 .context("Failed to make composite image")
605 .with_log()
606 .or_service_specific_exception(-1)?;
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000607
608 // Assemble disk images if needed.
609 let disks = config
610 .disks
611 .iter()
612 .map(|disk| {
613 assemble_disk_image(
614 disk,
615 &zero_filler_path,
616 &temporary_directory,
617 &mut next_temporary_image_id,
618 &mut indirect_files,
619 )
620 })
621 .collect::<Result<Vec<DiskFile>, _>>()?;
622
Akilesh Kailash05515dc2024-10-22 21:42:52 -0700623 let shared_paths = assemble_shared_paths(&config.sharedPaths, &temporary_directory)?;
624
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000625 let (cpus, host_cpu_topology) = match config.cpuTopology {
626 CpuTopology::MATCH_HOST => (None, true),
627 CpuTopology::ONE_CPU => (NonZeroU32::new(1), false),
628 val => {
Jiyong Park2227eaa2023-08-04 11:59:18 +0900629 return Err(anyhow!("Failed to parse CPU topology value {:?}", val))
630 .with_log()
631 .or_service_specific_exception(-1);
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000632 }
633 };
634
David Brazdil2dfefd12023-11-17 14:07:36 +0000635 let (vfio_devices, dtbo) = if !config.devices.is_empty() {
Inseob Kim6ef80972023-07-20 17:23:36 +0900636 let mut set = HashSet::new();
637 for device in config.devices.iter() {
Jiyong Park2227eaa2023-08-04 11:59:18 +0900638 let path = canonicalize(device)
639 .with_context(|| format!("can't canonicalize {device}"))
640 .or_service_specific_exception(-1)?;
Inseob Kim6ef80972023-07-20 17:23:36 +0900641 if !set.insert(path) {
Jiyong Park2227eaa2023-08-04 11:59:18 +0900642 return Err(anyhow!("duplicated device {device}"))
643 .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT);
Inseob Kim6ef80972023-07-20 17:23:36 +0900644 }
645 }
Jakob Vukalovicd42aa2c2023-11-09 16:04:00 +0000646 let devices = GLOBAL_SERVICE.bindDevicesToVfioDriver(&config.devices)?;
David Brazdil2dfefd12023-11-17 14:07:36 +0000647 let dtbo_file = File::from(
648 GLOBAL_SERVICE
649 .getDtboFile()?
650 .as_ref()
651 .try_clone()
Jaewan Kim39952072024-01-19 17:04:53 +0900652 .context("Failed to create VM DTBO from ParcelFileDescriptor")
David Brazdil2dfefd12023-11-17 14:07:36 +0000653 .or_binder_exception(ExceptionCode::BAD_PARCELABLE)?,
654 );
655 (devices, Some(dtbo_file))
Inseob Kim7307a892023-09-14 13:37:58 +0900656 } else {
David Brazdil2dfefd12023-11-17 14:07:36 +0000657 (vec![], None)
Inseob Kim7307a892023-09-14 13:37:58 +0900658 };
Jeongik Cha798401c2024-04-11 21:54:49 +0900659 let display_config = if cfg!(paravirtualized_devices) {
660 config
661 .displayConfig
662 .as_ref()
663 .map(DisplayConfig::new)
664 .transpose()
665 .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT)?
666 } else {
667 None
668 };
Jason Macnakea7b0fa2024-05-08 17:02:44 -0700669 let gpu_config = if cfg!(paravirtualized_devices) {
670 config
671 .gpuConfig
672 .as_ref()
673 .map(GpuConfig::new)
674 .transpose()
675 .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT)?
676 } else {
677 None
678 };
Jeongik Cha1df1c032024-04-03 16:03:12 +0900679
Jeongik Chac181be72024-03-27 00:18:30 +0900680 let input_device_options = if cfg!(paravirtualized_devices) {
681 config
682 .inputDevices
683 .iter()
684 .map(to_input_device_option_from)
685 .collect::<Result<Vec<InputDeviceOption>, _>>()
686 .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT)?
687 } else {
688 vec![]
689 };
690
Seungjae Yoo13af0b62024-05-20 14:15:13 +0900691 // Create TAP network interface if the VM supports network.
Seungjae Yoo986d7a42024-05-28 14:06:58 +0900692 let tap = if cfg!(network) && config.networkSupported {
Seungjae Yoo13af0b62024-05-20 14:15:13 +0900693 if *is_protected {
694 return Err(anyhow!("Network feature is not supported for pVM yet"))
695 .with_log()
696 .or_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION)?;
697 }
Seungjae Yoo986d7a42024-05-28 14:06:58 +0900698 Some(File::from(
699 GLOBAL_SERVICE
700 .createTapInterface(&get_this_pid().to_string())?
701 .as_ref()
702 .try_clone()
703 .context("Failed to get TAP interface from ParcelFileDescriptor")
704 .or_binder_exception(ExceptionCode::BAD_PARCELABLE)?,
705 ))
Seungjae Yoo13af0b62024-05-20 14:15:13 +0900706 } else {
707 None
708 };
Mu-Le Leeb2d5a312024-06-05 10:52:47 +0800709
710 let audio_config = if cfg!(paravirtualized_devices) {
711 config.audioConfig.as_ref().map(AudioConfig::new)
712 } else {
713 None
714 };
Seungjae Yoo13af0b62024-05-20 14:15:13 +0900715
Elie Kheirallah29d72912024-08-07 20:17:26 +0000716 let usb_config = config
717 .usbConfig
718 .as_ref()
719 .map(UsbConfig::new)
720 .unwrap_or(Ok(UsbConfig { controller: false }))
721 .or_binder_exception(ExceptionCode::BAD_PARCELABLE)?;
722
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000723 // Actually start the VM.
724 let crosvm_config = CrosvmConfig {
725 cid,
Seungjae Yoo62085c02022-08-12 04:44:52 +0000726 name: config.name.clone(),
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000727 bootloader: maybe_clone_file(&config.bootloader)?,
Alan Stokes185fe112023-01-10 16:20:55 +0000728 kernel,
729 initrd,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000730 disks,
Akilesh Kailash05515dc2024-10-22 21:42:52 -0700731 shared_paths,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000732 params: config.params.to_owned(),
733 protected: *is_protected,
Jaewan Kim61f86142023-03-28 15:12:52 +0900734 debug_config,
Frederick Mayle68d06e72024-07-12 17:18:11 -0700735 memory_mib: config
736 .memoryMib
737 .try_into()
738 .ok()
739 .and_then(NonZeroU32::new)
740 .unwrap_or(NonZeroU32::new(256).unwrap()),
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000741 cpus,
742 host_cpu_topology,
Jiyong Parke6fb1672023-06-26 16:45:55 +0900743 console_out_fd,
744 console_in_fd,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000745 log_fd,
Jiyong Parked180932023-02-24 19:55:41 +0900746 ramdump,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000747 indirect_files,
748 platform_version: parse_platform_version_req(&config.platformVersion)?,
Jiyong Parke6ed0f92022-06-22 00:13:00 +0900749 detect_hangup: is_app_config,
Nikita Ioffe5776f082023-02-10 21:38:26 +0000750 gdb_port,
Inseob Kim7307a892023-09-14 13:37:58 +0900751 vfio_devices,
David Brazdil2dfefd12023-11-17 14:07:36 +0000752 dtbo,
Shikha Panwar55e10ec2024-02-13 12:53:49 +0000753 device_tree_overlay,
Jeongik Cha1df1c032024-04-03 16:03:12 +0900754 display_config,
Jeongik Chac181be72024-03-27 00:18:30 +0900755 input_device_options,
Vincent Donnefort538a2c62024-03-20 16:01:10 +0000756 hugepages: config.hugePages,
Seungjae Yoo986d7a42024-05-28 14:06:58 +0900757 tap,
Yi-Yo Chiang8dd32552024-05-22 19:38:16 +0800758 console_input_device: config.consoleInputDevice.clone(),
David Dai23cff712024-06-13 19:23:45 +0000759 boost_uclamp: config.boostUclamp,
Jason Macnakea7b0fa2024-05-08 17:02:44 -0700760 gpu_config,
Mu-Le Leeb2d5a312024-06-05 10:52:47 +0800761 audio_config,
Jeongik Cha75057452024-09-03 11:26:04 +0900762 no_balloon: config.noBalloon,
Elie Kheirallah29d72912024-08-07 20:17:26 +0000763 usb_config,
Elie Kheirallah5c807a22024-09-23 20:40:42 +0000764 dump_dt_fd,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000765 };
766 let instance = Arc::new(
David Brazdil528e0472022-10-10 15:06:02 +0100767 VmInstance::new(
768 crosvm_config,
769 temporary_directory,
770 requester_uid,
771 requester_debug_pid,
772 vm_context,
773 )
Jiyong Park2227eaa2023-08-04 11:59:18 +0900774 .with_context(|| format!("Failed to create VM with config {:?}", config))
775 .with_log()
776 .or_service_specific_exception(-1)?,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000777 );
778 state.add_vm(Arc::downgrade(&instance));
779 Ok(VirtualMachine::create(instance))
780 }
Jiyong Park8611a6c2021-07-09 18:17:44 +0900781}
782
Alan Stokesfda70842023-12-20 17:50:14 +0000783/// Returns whether a VM config represents a "custom" virtual machine, which requires the
784/// USE_CUSTOM_VIRTUAL_MACHINE.
785fn is_custom_config(config: &VirtualMachineConfig) -> bool {
786 match config {
787 // Any raw (non-Microdroid) VM is considered custom.
788 VirtualMachineConfig::RawConfig(_) => true,
789 VirtualMachineConfig::AppConfig(config) => {
790 // Some features are reserved for platform apps only, even when using
791 // VirtualMachineAppConfig. Almost all of these features are grouped in the
792 // CustomConfig struct:
793 // - controlling CPUs;
794 // - gdbPort is set, meaning that crosvm will start a gdb server;
795 // - using anything other than the default kernel;
796 // - specifying devices to be assigned.
797 if config.customConfig.is_some() {
798 true
799 } else {
800 // Additional custom features not included in CustomConfig:
801 // - specifying a config file;
Alan Stokes736f6822024-02-16 16:08:00 +0000802 // - specifying extra APKs;
803 // - specifying an OS other than Microdroid.
Inseob Kim89b24592024-02-23 18:59:43 +0900804 (match &config.payload {
Alan Stokesfda70842023-12-20 17:50:14 +0000805 Payload::ConfigPath(_) => true,
Inseob Kim89b24592024-02-23 18:59:43 +0900806 Payload::PayloadConfig(payload_config) => !payload_config.extraApks.is_empty(),
807 }) || config.osName != MICRODROID_OS_NAME
Alan Stokesfda70842023-12-20 17:50:14 +0000808 }
809 }
810 }
811}
812
Seungjae Yoo14e60182024-02-21 13:28:31 +0900813fn extract_vendor_hashtree_digest(config: &VirtualMachineConfig) -> Result<Option<Vec<u8>>> {
814 let VirtualMachineConfig::AppConfig(config) = config else {
815 return Ok(None);
816 };
817 let Some(custom_config) = &config.customConfig else {
818 return Ok(None);
819 };
820 let Some(file) = custom_config.vendorImage.as_ref() else {
821 return Ok(None);
822 };
823
824 let file = clone_file(file)?;
825 let size =
826 file.metadata().context("Failed to get metadata from microdroid vendor image")?.len();
827 let vbmeta = VbMetaImage::verify_reader_region(&file, 0, size)
828 .context("Failed to get vbmeta from microdroid-vendor.img")?;
829
830 for descriptor in vbmeta.descriptors()?.iter() {
831 if let vbmeta::Descriptor::Hashtree(_) = descriptor {
832 let root_digest = hex::encode(descriptor.to_hashtree()?.root_digest());
833 return Ok(Some(root_digest.as_bytes().to_vec()));
834 }
835 }
836 Err(anyhow!("No hashtree digest is extracted from microdroid vendor image"))
837}
838
Nikita Ioffeb2c6f3a2024-06-06 12:51:53 +0000839fn maybe_create_device_tree_overlay(
840 config: &VirtualMachineConfig,
841 temporary_directory: &Path,
842) -> binder::Result<Option<File>> {
843 // Currently, VirtMgr adds the host copy of reference DT & untrusted properties
844 // (e.g. instance-id)
845 let host_ref_dt = Path::new(VM_REFERENCE_DT_ON_HOST_PATH);
846 let host_ref_dt = if host_ref_dt.exists()
847 && read_dir(host_ref_dt).or_service_specific_exception(-1)?.next().is_some()
848 {
849 Some(host_ref_dt)
850 } else {
851 warn!("VM reference DT doesn't exist in host DT");
852 None
853 };
854
855 let vendor_hashtree_digest = extract_vendor_hashtree_digest(config)
856 .context("Failed to extract vendor hashtree digest")
857 .or_service_specific_exception(-1)?;
858
859 let trusted_props = if let Some(ref vendor_hashtree_digest) = vendor_hashtree_digest {
860 info!(
861 "Passing vendor hashtree digest to pvmfw. This will be rejected if it doesn't \
862 match the trusted digest in the pvmfw config, causing the VM to fail to start."
863 );
864 vec![(cstr!("vendor_hashtree_descriptor_root_digest"), vendor_hashtree_digest.as_slice())]
865 } else {
866 vec![]
867 };
868
869 let instance_id;
870 let mut untrusted_props = Vec::with_capacity(2);
871 if cfg!(llpvm_changes) {
872 instance_id = extract_instance_id(config);
873 untrusted_props.push((cstr!("instance-id"), &instance_id[..]));
874 let want_updatable = extract_want_updatable(config);
875 if want_updatable && is_secretkeeper_supported() {
876 // Let guest know that it can defer rollback protection to Secretkeeper by setting
877 // an empty property in untrusted node in DT. This enables Updatable VMs.
878 untrusted_props.push((cstr!("defer-rollback-protection"), &[]))
879 }
880 }
881
882 let device_tree_overlay = if host_ref_dt.is_some()
883 || !untrusted_props.is_empty()
884 || !trusted_props.is_empty()
885 {
886 let dt_output = temporary_directory.join(VM_DT_OVERLAY_PATH);
887 let mut data = [0_u8; VM_DT_OVERLAY_MAX_SIZE];
888 let fdt =
889 create_device_tree_overlay(&mut data, host_ref_dt, &untrusted_props, &trusted_props)
890 .map_err(|e| anyhow!("Failed to create DT overlay, {e:?}"))
891 .or_service_specific_exception(-1)?;
892 fs::write(&dt_output, fdt.as_slice()).or_service_specific_exception(-1)?;
893 Some(File::open(dt_output).or_service_specific_exception(-1)?)
894 } else {
895 None
896 };
897 Ok(device_tree_overlay)
898}
899
Andrew Walbranfbb39d22021-07-28 17:01:25 +0000900fn write_zero_filler(zero_filler_path: &Path) -> Result<()> {
Jooyung Han95884632021-07-06 22:27:54 +0900901 let file = OpenOptions::new()
902 .create_new(true)
903 .read(true)
904 .write(true)
905 .open(zero_filler_path)
906 .with_context(|| "Failed to create zero.img")?;
907 file.set_len(ZERO_FILLER_SIZE)?;
Andrew Walbranfbb39d22021-07-28 17:01:25 +0000908 Ok(())
Jooyung Han95884632021-07-06 22:27:54 +0900909}
910
David Brazdilf50c7a62023-04-19 14:22:42 +0000911fn format_as_android_vm_instance(part: &mut dyn Write) -> std::io::Result<()> {
912 part.write_all(ANDROID_VM_INSTANCE_MAGIC.as_bytes())?;
913 part.write_all(&ANDROID_VM_INSTANCE_VERSION.to_le_bytes())?;
914 part.flush()
915}
916
917fn format_as_encryptedstore(part: &mut dyn Write) -> std::io::Result<()> {
918 part.write_all(UNFORMATTED_STORAGE_MAGIC.as_bytes())?;
919 part.flush()
920}
921
922fn round_up(input: u64, granularity: u64) -> u64 {
923 if granularity == 0 {
924 return input;
925 }
926 // If the input is absurdly large we round down instead of up; it's going to fail anyway.
927 let result = input.checked_add(granularity - 1).unwrap_or(input);
928 (result / granularity) * granularity
929}
930
Jeongik Chac181be72024-03-27 00:18:30 +0900931fn to_input_device_option_from(input_device: &InputDevice) -> Result<InputDeviceOption> {
932 Ok(match input_device {
933 InputDevice::SingleTouch(single_touch) => InputDeviceOption::SingleTouch {
934 file: clone_file(single_touch.pfd.as_ref().ok_or(anyhow!("pfd should have value"))?)?,
935 height: u32::try_from(single_touch.height)?,
936 width: u32::try_from(single_touch.width)?,
937 name: if !single_touch.name.is_empty() {
938 Some(single_touch.name.clone())
939 } else {
940 None
941 },
942 },
943 InputDevice::EvDev(evdev) => InputDeviceOption::EvDev(clone_file(
944 evdev.pfd.as_ref().ok_or(anyhow!("pfd should have value"))?,
945 )?),
Jeongik Cha985b6402024-04-15 18:13:00 +0900946 InputDevice::Keyboard(keyboard) => InputDeviceOption::Keyboard(clone_file(
947 keyboard.pfd.as_ref().ok_or(anyhow!("pfd should have value"))?,
948 )?),
Jeongik Cha343894e2024-05-17 20:39:31 +0900949 InputDevice::Mouse(mouse) => InputDeviceOption::Mouse(clone_file(
950 mouse.pfd.as_ref().ok_or(anyhow!("pfd should have value"))?,
951 )?),
Jiyong Park6c1b9f02024-07-05 16:21:10 +0900952 InputDevice::Switches(switches) => InputDeviceOption::Switches(clone_file(
953 switches.pfd.as_ref().ok_or(anyhow!("pfd should have value"))?,
954 )?),
Jeongik Cha112a2682024-07-09 12:28:45 +0900955 InputDevice::Trackpad(trackpad) => InputDeviceOption::MultiTouchTrackpad {
956 file: clone_file(trackpad.pfd.as_ref().ok_or(anyhow!("pfd should have value"))?)?,
957 height: u32::try_from(trackpad.height)?,
958 width: u32::try_from(trackpad.width)?,
959 name: if !trackpad.name.is_empty() { Some(trackpad.name.clone()) } else { None },
960 },
Jeongik Cha10494912024-07-16 11:19:50 +0900961 InputDevice::MultiTouch(multi_touch) => InputDeviceOption::MultiTouch {
962 file: clone_file(multi_touch.pfd.as_ref().ok_or(anyhow!("pfd should have value"))?)?,
963 height: u32::try_from(multi_touch.height)?,
964 width: u32::try_from(multi_touch.width)?,
965 name: if !multi_touch.name.is_empty() { Some(multi_touch.name.clone()) } else { None },
966 },
Jeongik Chac181be72024-03-27 00:18:30 +0900967 })
968}
Akilesh Kailash05515dc2024-10-22 21:42:52 -0700969
970fn assemble_shared_paths(
971 shared_paths: &[SharedPath],
972 temporary_directory: &Path,
973) -> Result<Vec<SharedPathConfig>, Status> {
974 if shared_paths.is_empty() {
975 return Ok(Vec::new()); // Return an empty vector if shared_paths is empty
976 }
977
978 shared_paths
979 .iter()
980 .map(|path| {
981 Ok(SharedPathConfig {
982 path: path.sharedPath.clone(),
983 host_uid: path.hostUid,
984 host_gid: path.hostGid,
985 guest_uid: path.guestUid,
986 guest_gid: path.guestGid,
987 mask: path.mask,
988 tag: path.tag.clone(),
989 socket_path: temporary_directory.join(&path.socket).to_string_lossy().to_string(),
990 })
991 })
992 .collect()
993}
994
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000995/// Given the configuration for a disk image, assembles the `DiskFile` to pass to crosvm.
996///
997/// This may involve assembling a composite disk from a set of partition images.
998fn assemble_disk_image(
999 disk: &DiskImage,
Jooyung Han95884632021-07-06 22:27:54 +09001000 zero_filler_path: &Path,
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +00001001 temporary_directory: &Path,
1002 next_temporary_image_id: &mut u64,
1003 indirect_files: &mut Vec<File>,
Andrew Walbran806f1542021-06-10 14:07:12 +00001004) -> Result<DiskFile, Status> {
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +00001005 let image = if !disk.partitions.is_empty() {
1006 if disk.image.is_some() {
1007 warn!("DiskImage {:?} contains both image and partitions.", disk);
Jiyong Park2227eaa2023-08-04 11:59:18 +09001008 return Err(anyhow!("DiskImage contains both image and partitions"))
1009 .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT);
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +00001010 }
1011
Andrew Walbran3eca16c2021-06-14 11:15:14 +00001012 let composite_image_filenames =
1013 make_composite_image_filenames(temporary_directory, next_temporary_image_id);
1014 let (image, partition_files) = make_composite_image(
1015 &disk.partitions,
Jooyung Han95884632021-07-06 22:27:54 +09001016 zero_filler_path,
Andrew Walbran3eca16c2021-06-14 11:15:14 +00001017 &composite_image_filenames.composite,
1018 &composite_image_filenames.header,
1019 &composite_image_filenames.footer,
1020 )
Jiyong Park2227eaa2023-08-04 11:59:18 +09001021 .with_context(|| format!("Failed to make composite disk image with config {:?}", disk))
1022 .with_log()
1023 .or_service_specific_exception(-1)?;
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +00001024
1025 // Pass the file descriptors for the various partition files to crosvm when it
1026 // is run.
1027 indirect_files.extend(partition_files);
1028
1029 image
1030 } else if let Some(image) = &disk.image {
1031 clone_file(image)?
1032 } else {
1033 warn!("DiskImage {:?} didn't contain image or partitions.", disk);
Jiyong Park2227eaa2023-08-04 11:59:18 +09001034 return Err(anyhow!("DiskImage didn't contain image or partitions."))
1035 .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT);
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +00001036 };
1037
1038 Ok(DiskFile { image, writable: disk.writable })
1039}
1040
Nikita Ioffeaa6858c2023-07-04 01:37:41 +01001041fn append_kernel_param(param: &str, vm_config: &mut VirtualMachineRawConfig) {
1042 if let Some(ref mut params) = vm_config.params {
1043 params.push(' ');
1044 params.push_str(param)
1045 } else {
1046 vm_config.params = Some(param.to_owned())
1047 }
1048}
1049
Inseob Kim46257382024-01-03 15:41:22 +09001050fn extract_os_name_from_config_path(config: &Path) -> Option<String> {
1051 if config.extension()?.to_str()? != "json" {
1052 return None;
Inseob Kim172f9eb2023-11-06 17:02:08 +09001053 }
Inseob Kim46257382024-01-03 15:41:22 +09001054
1055 Some(config.with_extension("").file_name()?.to_str()?.to_owned())
1056}
1057
1058fn extract_os_names_from_configs(config_glob_pattern: &str) -> Result<HashSet<String>> {
1059 let configs = glob(config_glob_pattern)?.collect::<Result<Vec<_>, _>>()?;
1060 let os_names =
1061 configs.iter().filter_map(|x| extract_os_name_from_config_path(x)).collect::<HashSet<_>>();
1062
1063 Ok(os_names)
1064}
1065
1066fn get_supported_os_names() -> Result<HashSet<String>> {
1067 if !cfg!(vendor_modules) {
1068 return Ok(iter::once(MICRODROID_OS_NAME.to_owned()).collect());
1069 }
1070
1071 extract_os_names_from_configs("/apex/com.android.virt/etc/microdroid*.json")
1072}
1073
1074fn is_valid_os(os_name: &str) -> bool {
1075 SUPPORTED_OS_NAMES.contains(os_name)
Inseob Kim172f9eb2023-11-06 17:02:08 +09001076}
1077
Nikita Ioffe2cb75cf2024-06-04 16:43:56 +00001078fn uses_gki_kernel(config: &VirtualMachineConfig) -> bool {
1079 if !cfg!(vendor_modules) {
1080 return false;
1081 }
1082 match config {
1083 VirtualMachineConfig::RawConfig(_) => false,
1084 VirtualMachineConfig::AppConfig(config) => config.osName.starts_with("microdroid_gki-"),
1085 }
1086}
1087
Jooyung Han21e9b922021-06-26 04:14:16 +09001088fn load_app_config(
1089 config: &VirtualMachineAppConfig,
Jaewan Kim61f86142023-03-28 15:12:52 +09001090 debug_config: &DebugConfig,
Jooyung Han21e9b922021-06-26 04:14:16 +09001091 temporary_directory: &Path,
Jooyung Hanadfb76c2021-06-28 17:29:30 +09001092) -> Result<VirtualMachineRawConfig> {
Andrew Walbrancc0db522021-07-12 17:03:42 +00001093 let apk_file = clone_file(config.apk.as_ref().unwrap())?;
1094 let idsig_file = clone_file(config.idsig.as_ref().unwrap())?;
Jiyong Park8d081812021-07-23 17:45:04 +09001095 let instance_file = clone_file(config.instanceImage.as_ref().unwrap())?;
Jooyung Han21e9b922021-06-26 04:14:16 +09001096
Shikha Panwar22e70452022-10-10 18:32:55 +00001097 let storage_image = if let Some(file) = config.encryptedStorageImage.as_ref() {
1098 Some(clone_file(file)?)
1099 } else {
1100 None
1101 };
1102
Alan Stokesfda70842023-12-20 17:50:14 +00001103 let vm_payload_config;
1104 let extra_apk_files: Vec<_>;
1105 match &config.payload {
Alan Stokes0d1ef782022-09-27 13:46:35 +01001106 Payload::ConfigPath(config_path) => {
Alan Stokesfda70842023-12-20 17:50:14 +00001107 vm_payload_config =
1108 load_vm_payload_config_from_file(&apk_file, config_path.as_str())
1109 .with_context(|| format!("Couldn't read config from {}", config_path))?;
1110 extra_apk_files = vm_payload_config
1111 .extra_apks
1112 .iter()
1113 .enumerate()
1114 .map(|(i, apk)| {
1115 File::open(PathBuf::from(&apk.path))
1116 .with_context(|| format!("Failed to open extra apk #{i} {}", apk.path))
1117 })
1118 .collect::<Result<_>>()?;
Alan Stokes0d1ef782022-09-27 13:46:35 +01001119 }
Alan Stokesfda70842023-12-20 17:50:14 +00001120 Payload::PayloadConfig(payload_config) => {
1121 vm_payload_config = create_vm_payload_config(payload_config)?;
1122 extra_apk_files =
1123 payload_config.extraApks.iter().map(clone_file).collect::<binder::Result<_>>()?;
1124 }
Alan Stokes0d1ef782022-09-27 13:46:35 +01001125 };
Jooyung Han21e9b922021-06-26 04:14:16 +09001126
Inseob Kim89b24592024-02-23 18:59:43 +09001127 let payload_config_os = vm_payload_config.os.name.as_str();
1128 if !payload_config_os.is_empty() && payload_config_os != "microdroid" {
1129 bail!("'os' in payload config is deprecated");
1130 }
1131
Inseob Kim172f9eb2023-11-06 17:02:08 +09001132 // For now, the only supported OS is Microdroid and Microdroid GKI
Inseob Kim89b24592024-02-23 18:59:43 +09001133 let os_name = config.osName.as_str();
Inseob Kim172f9eb2023-11-06 17:02:08 +09001134 if !is_valid_os(os_name) {
Andrew Walbrancc0db522021-07-12 17:03:42 +00001135 bail!("Unknown OS \"{}\"", os_name);
Jooyung Han35edb8f2021-07-01 16:17:16 +09001136 }
Andrew Walbrancc0db522021-07-12 17:03:42 +00001137
1138 // It is safe to construct a filename based on the os_name because we've already checked that it
1139 // is one of the allowed values.
Jooyung Han21e9b922021-06-26 04:14:16 +09001140 let vm_config_path = PathBuf::from(format!("/apex/com.android.virt/etc/{}.json", os_name));
1141 let vm_config_file = File::open(vm_config_path)?;
Andrew Walbrancc0db522021-07-12 17:03:42 +00001142 let mut vm_config = VmConfig::load(&vm_config_file)?.to_parcelable()?;
Jooyung Han21e9b922021-06-26 04:14:16 +09001143
Nikita Ioffea0eb5ee2023-06-26 18:18:21 +01001144 if let Some(custom_config) = &config.customConfig {
1145 if let Some(file) = custom_config.customKernelImage.as_ref() {
1146 vm_config.kernel = Some(ParcelFileDescriptor::new(clone_file(file)?))
1147 }
Nikita Ioffea0eb5ee2023-06-26 18:18:21 +01001148 vm_config.gdbPort = custom_config.gdbPort;
Nikita Ioffe5dfddf22023-06-29 16:11:26 +01001149
1150 if let Some(file) = custom_config.vendorImage.as_ref() {
Nikita Ioffeaa6858c2023-07-04 01:37:41 +01001151 add_microdroid_vendor_image(clone_file(file)?, &mut vm_config);
Pechetty Sravani (xWF)faabfbd2024-09-24 15:27:48 +00001152 append_kernel_param("androidboot.microdroid.mount_vendor=1", &mut vm_config)
Nikita Ioffe5dfddf22023-06-29 16:11:26 +01001153 }
Inseob Kim6ef80972023-07-20 17:23:36 +09001154
Chris Wailes6177c662024-05-09 14:37:44 -07001155 vm_config.devices.clone_from(&custom_config.devices);
Seungjae Yoo13af0b62024-05-20 14:15:13 +09001156 vm_config.networkSupported = custom_config.networkSupported;
Nikita Ioffeb4268b32024-09-03 10:23:14 +00001157
1158 for param in custom_config.extraKernelCmdlineParams.iter() {
1159 append_kernel_param(param, &mut vm_config);
1160 }
Nikita Ioffe26c35ed2023-06-05 17:49:08 +01001161 }
1162
Nikita Ioffed5846dc2024-11-01 18:44:45 +00001163 // Unfortunately specifying page_shift = 14 in bootconfig doesn't enable 16k pages emulation,
1164 // so we need to provide it in the kernel cmdline.
1165 // TODO(b/376901009): remove this after passing page_shift in bootconfig is supported.
1166 if os_name.ends_with("_16k") && cfg!(target_arch = "x86_64") {
1167 append_kernel_param("page_shift=14", &mut vm_config);
1168 }
1169
Andrew Walbrancc045902021-07-27 16:06:17 +00001170 if config.memoryMib > 0 {
1171 vm_config.memoryMib = config.memoryMib;
Andrew Walbran45bcb0c2021-07-14 15:02:06 +00001172 }
1173
Chris Wailes6177c662024-05-09 14:37:44 -07001174 vm_config.name.clone_from(&config.name);
Andrew Walbran3994f002022-01-27 17:33:45 +00001175 vm_config.protectedVm = config.protectedVm;
David Brazdil7d1e5ec2023-02-06 17:56:29 +00001176 vm_config.cpuTopology = config.cpuTopology;
Vincent Donnefort538a2c62024-03-20 16:01:10 +00001177 vm_config.hugePages = config.hugePages || vm_payload_config.hugepages;
David Dai23cff712024-06-13 19:23:45 +00001178 vm_config.boostUclamp = config.boostUclamp;
Jiyong Park032615f2022-01-10 13:55:34 +09001179
Shikha Panwar22e70452022-10-10 18:32:55 +00001180 // Microdroid takes additional init ramdisk & (optionally) storage image
Inseob Kim172f9eb2023-11-06 17:02:08 +09001181 add_microdroid_system_images(config, instance_file, storage_image, os_name, &mut vm_config)?;
Shikha Panwar22e70452022-10-10 18:32:55 +00001182
1183 // Include Microdroid payload disk (contains apks, idsigs) in vm config
1184 add_microdroid_payload_images(
Alan Stokes0d1ef782022-09-27 13:46:35 +01001185 config,
Jaewan Kim61f86142023-03-28 15:12:52 +09001186 debug_config,
Alan Stokes0d1ef782022-09-27 13:46:35 +01001187 temporary_directory,
1188 apk_file,
1189 idsig_file,
Alan Stokesfda70842023-12-20 17:50:14 +00001190 extra_apk_files,
Alan Stokes0d1ef782022-09-27 13:46:35 +01001191 &vm_payload_config,
1192 &mut vm_config,
1193 )?;
Jooyung Han21e9b922021-06-26 04:14:16 +09001194
Andrew Walbrancc0db522021-07-12 17:03:42 +00001195 Ok(vm_config)
Jooyung Han21e9b922021-06-26 04:14:16 +09001196}
1197
Inseob Kimff48a7c2024-02-26 22:07:21 +09001198fn check_partition_for_file(fd: &ParcelFileDescriptor) -> Result<()> {
1199 let path = format!("/proc/self/fd/{}", fd.as_raw_fd());
1200 let link = fs::read_link(&path).context(format!("can't read_link {path}"))?;
1201
1202 // microdroid vendor image is OK
1203 if cfg!(vendor_modules) && link == Path::new("/vendor/etc/avf/microdroid/microdroid_vendor.img")
1204 {
1205 return Ok(());
1206 }
1207
1208 if link.starts_with("/vendor") || link.starts_with("/odm") {
1209 bail!("vendor or odm file {} can't be used for VM", link.display());
1210 }
1211
1212 Ok(())
1213}
1214
1215fn check_partitions_for_files(config: &VirtualMachineRawConfig) -> Result<()> {
1216 config
1217 .disks
1218 .iter()
1219 .flat_map(|disk| disk.partitions.iter())
1220 .filter_map(|partition| partition.image.as_ref())
1221 .try_for_each(check_partition_for_file)?;
1222
1223 config.kernel.as_ref().map_or(Ok(()), check_partition_for_file)?;
1224 config.initrd.as_ref().map_or(Ok(()), check_partition_for_file)?;
1225 config.bootloader.as_ref().map_or(Ok(()), check_partition_for_file)?;
1226
1227 Ok(())
1228}
1229
Alan Stokes0d1ef782022-09-27 13:46:35 +01001230fn load_vm_payload_config_from_file(apk_file: &File, config_path: &str) -> Result<VmPayloadConfig> {
1231 let mut apk_zip = ZipArchive::new(apk_file)?;
1232 let config_file = apk_zip.by_name(config_path)?;
1233 Ok(serde_json::from_reader(config_file)?)
1234}
1235
Alan Stokes8f12f2b2023-01-09 09:19:20 +00001236fn create_vm_payload_config(
1237 payload_config: &VirtualMachinePayloadConfig,
1238) -> Result<VmPayloadConfig> {
Alan Stokes0d1ef782022-09-27 13:46:35 +01001239 // There isn't an actual config file. Construct a synthetic VmPayloadConfig from the explicit
1240 // parameters we've been given. Microdroid will do something equivalent inside the VM using the
1241 // payload config that we send it via the metadata file.
Alan Stokes8f12f2b2023-01-09 09:19:20 +00001242
1243 let payload_binary_name = &payload_config.payloadBinaryName;
1244 if payload_binary_name.contains('/') {
1245 bail!("Payload binary name must not specify a path: {payload_binary_name}");
1246 }
1247
1248 let task = Task { type_: TaskType::MicrodroidLauncher, command: payload_binary_name.clone() };
Alan Stokesfda70842023-12-20 17:50:14 +00001249
1250 // The VM only cares about how many there are, these names are actually ignored.
1251 let extra_apk_count = payload_config.extraApks.len();
1252 let extra_apks =
1253 (0..extra_apk_count).map(|i| ApkConfig { path: format!("extra-apk-{i}") }).collect();
1254
Inseob Kim89b24592024-02-23 18:59:43 +09001255 Ok(VmPayloadConfig { task: Some(task), extra_apks, ..Default::default() })
Alan Stokes0d1ef782022-09-27 13:46:35 +01001256}
1257
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +00001258/// Generates a unique filename to use for a composite disk image.
Andrew Walbran3eca16c2021-06-14 11:15:14 +00001259fn make_composite_image_filenames(
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +00001260 temporary_directory: &Path,
1261 next_temporary_image_id: &mut u64,
Andrew Walbran3eca16c2021-06-14 11:15:14 +00001262) -> CompositeImageFilenames {
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +00001263 let id = *next_temporary_image_id;
1264 *next_temporary_image_id += 1;
Andrew Walbran3eca16c2021-06-14 11:15:14 +00001265 CompositeImageFilenames {
1266 composite: temporary_directory.join(format!("composite-{}.img", id)),
1267 header: temporary_directory.join(format!("composite-{}-header.img", id)),
1268 footer: temporary_directory.join(format!("composite-{}-footer.img", id)),
1269 }
1270}
1271
1272/// Filenames for a composite disk image, including header and footer partitions.
1273#[derive(Clone, Debug, Eq, PartialEq)]
1274struct CompositeImageFilenames {
1275 /// The composite disk image itself.
1276 composite: PathBuf,
1277 /// The header partition image.
1278 header: PathBuf,
1279 /// The footer partition image.
1280 footer: PathBuf,
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +00001281}
1282
Jiyong Park753553b2021-07-12 21:21:09 +09001283/// Checks whether the caller has a specific permission
1284fn check_permission(perm: &str) -> binder::Result<()> {
Inseob Kimecde8c02024-09-03 13:11:08 +09001285 if cfg!(early) {
1286 // Skip permission check for early VMs, in favor of SELinux
1287 return Ok(());
1288 }
David Brazdil1f530702022-10-03 12:18:10 +01001289 let calling_pid = get_calling_pid();
1290 let calling_uid = get_calling_uid();
Jiyong Park753553b2021-07-12 21:21:09 +09001291 // Root can do anything
1292 if calling_uid == 0 {
1293 return Ok(());
1294 }
1295 let perm_svc: Strong<dyn IPermissionController::IPermissionController> =
Frederick Mayleb2a02872024-05-08 13:35:12 -07001296 binder::wait_for_interface("permission")?;
Jiyong Park753553b2021-07-12 21:21:09 +09001297 if perm_svc.checkPermission(perm, calling_pid, calling_uid as i32)? {
Andrew Walbran806f1542021-06-10 14:07:12 +00001298 Ok(())
1299 } else {
Jiyong Park2227eaa2023-08-04 11:59:18 +09001300 Err(anyhow!("does not have the {} permission", perm))
1301 .or_binder_exception(ExceptionCode::SECURITY)
Andrew Walbran806f1542021-06-10 14:07:12 +00001302 }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001303}
1304
Jiyong Park753553b2021-07-12 21:21:09 +09001305/// Check whether the caller of the current Binder method is allowed to manage VMs
1306fn check_manage_access() -> binder::Result<()> {
1307 check_permission("android.permission.MANAGE_VIRTUAL_MACHINE")
1308}
1309
Inseob Kim1119d702022-05-02 18:01:58 +09001310/// Check whether the caller of the current Binder method is allowed to create custom VMs
1311fn check_use_custom_virtual_machine() -> binder::Result<()> {
1312 check_permission("android.permission.USE_CUSTOM_VIRTUAL_MACHINE")
1313}
1314
Alan Stokes185fe112023-01-10 16:20:55 +00001315/// Return whether a partition is exempt from selinux label checks, because we know that it does
1316/// not contain code and is likely to be generated in an app-writable directory.
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001317fn is_safe_app_partition(label: &str) -> bool {
Shikha Panwara2ff8c52022-11-30 19:25:46 +00001318 // See add_microdroid_system_images & add_microdroid_payload_images in payload.rs.
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001319 label == "vm-instance"
Shikha Panwara2ff8c52022-11-30 19:25:46 +00001320 || label == "encryptedstore"
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001321 || label == "microdroid-apk-idsig"
1322 || label == "payload-metadata"
1323 || label.starts_with("extra-idsig-")
1324}
1325
Alice Wangc206b9b2023-08-28 14:13:51 +00001326/// Returns whether a partition with the given label is safe for a raw config VM.
1327fn is_safe_raw_partition(label: &str) -> bool {
1328 label == "vm-instance"
1329}
1330
Alan Stokes185fe112023-01-10 16:20:55 +00001331/// Check that a file SELinux label is acceptable.
1332///
1333/// We only want to allow code in a VM to be sourced from places that apps, and the
Seungjae Yoo2b74c442023-11-15 18:05:07 +09001334/// system or vendor, do not have write access to.
Alan Stokes185fe112023-01-10 16:20:55 +00001335///
1336/// Note that sepolicy must also grant read access for these types to both virtualization
1337/// service and crosvm.
1338///
1339/// App private data files are deliberately excluded, to avoid arbitrary payloads being run on
1340/// user devices (W^X).
1341fn check_label_is_allowed(context: &SeContext) -> Result<()> {
1342 match context.selinux_type()? {
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001343 | "apk_data_file" // APKs of an installed app
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001344 | "shell_data_file" // test files created via adb shell
Alan Stokesfe4bb0c2023-03-20 14:15:36 +00001345 | "staging_data_file" // updated/staged APEX images
1346 | "system_file" // immutable dm-verity protected partition
1347 | "virtualizationservice_data_file" // files created by VS / VirtMgr
Seungjae Yoo2b74c442023-11-15 18:05:07 +09001348 | "vendor_microdroid_file" // immutable dm-verity protected partition (/vendor/etc/avf/microdroid/.*)
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001349 => Ok(()),
Alan Stokes185fe112023-01-10 16:20:55 +00001350 _ => bail!("Label {} is not allowed", context),
Jiyong Park029977d2021-11-24 21:56:49 +09001351 }
1352}
1353
Alan Stokes185fe112023-01-10 16:20:55 +00001354fn check_label_for_partition(partition: &Partition) -> Result<()> {
1355 let file = partition.image.as_ref().unwrap().as_ref();
1356 check_label_is_allowed(&getfilecon(file)?)
1357 .with_context(|| format!("Partition {} invalid", &partition.label))
1358}
1359
1360fn check_label_for_kernel_files(kernel: &Option<File>, initrd: &Option<File>) -> Result<()> {
1361 if let Some(f) = kernel {
1362 check_label_for_file(f, "kernel")?;
1363 }
1364 if let Some(f) = initrd {
1365 check_label_for_file(f, "initrd")?;
1366 }
1367 Ok(())
1368}
1369fn check_label_for_file(file: &File, name: &str) -> Result<()> {
1370 check_label_is_allowed(&getfilecon(file)?).with_context(|| format!("{} file invalid", name))
1371}
1372
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001373/// Implementation of the AIDL `IVirtualMachine` interface. Used as a handle to a VM.
1374#[derive(Debug)]
1375struct VirtualMachine {
1376 instance: Arc<VmInstance>,
1377}
1378
1379impl VirtualMachine {
Devin Moore407df8f2024-08-27 19:39:16 +00001380 fn create(instance: Arc<VmInstance>) -> Strong<dyn IVirtualMachine::IVirtualMachine> {
David Brazdil4b4c5102022-12-19 22:56:20 +00001381 BnVirtualMachine::new_binder(VirtualMachine { instance }, BinderFeatures::default())
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001382 }
1383}
1384
1385impl Interface for VirtualMachine {}
1386
Devin Moore407df8f2024-08-27 19:39:16 +00001387impl IVirtualMachine::IVirtualMachine for VirtualMachine {
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001388 fn getCid(&self) -> binder::Result<i32> {
Jiyong Park753553b2021-07-12 21:21:09 +09001389 // Don't check permission. The owner of the VM might have passed this binder object to
1390 // others.
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001391 Ok(self.instance.cid as i32)
1392 }
Andrew Walbrandae07162021-03-12 17:05:20 +00001393
Andrew Walbran6b650662021-09-07 13:13:23 +00001394 fn getState(&self) -> binder::Result<VirtualMachineState> {
Jiyong Park753553b2021-07-12 21:21:09 +09001395 // Don't check permission. The owner of the VM might have passed this binder object to
1396 // others.
Andrew Walbran6b650662021-09-07 13:13:23 +00001397 Ok(get_state(&self.instance))
Andrew Walbrandae07162021-03-12 17:05:20 +00001398 }
1399
1400 fn registerCallback(
1401 &self,
1402 callback: &Strong<dyn IVirtualMachineCallback>,
1403 ) -> binder::Result<()> {
Jiyong Park753553b2021-07-12 21:21:09 +09001404 // Don't check permission. The owner of the VM might have passed this binder object to
1405 // others.
1406 //
Andrew Walbrandae07162021-03-12 17:05:20 +00001407 // TODO: Should this give an error if the VM is already dead?
1408 self.instance.callbacks.add(callback.clone());
1409 Ok(())
1410 }
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001411
Andrew Walbranf8d94112021-09-07 11:45:36 +00001412 fn start(&self) -> binder::Result<()> {
Jiyong Park2227eaa2023-08-04 11:59:18 +09001413 self.instance
1414 .start()
1415 .with_context(|| format!("Error starting VM with CID {}", self.instance.cid))
1416 .with_log()
1417 .or_service_specific_exception(-1)
Andrew Walbranf8d94112021-09-07 11:45:36 +00001418 }
1419
Inseob Kima446f802022-07-11 19:46:37 +09001420 fn stop(&self) -> binder::Result<()> {
Jiyong Park2227eaa2023-08-04 11:59:18 +09001421 self.instance
1422 .kill()
1423 .with_context(|| format!("Error stopping VM with CID {}", self.instance.cid))
1424 .with_log()
1425 .or_service_specific_exception(-1)
Inseob Kima446f802022-07-11 19:46:37 +09001426 }
1427
Keir Fraser48221ba2024-07-12 14:05:02 +00001428 fn getMemoryBalloon(&self) -> binder::Result<i64> {
1429 let balloon = self
1430 .instance
1431 .get_memory_balloon()
1432 .with_context(|| format!("Error getting balloon for VM with CID {}", self.instance.cid))
1433 .with_log()
1434 .or_service_specific_exception(-1)?;
1435 Ok(balloon.try_into().unwrap())
1436 }
1437
1438 fn setMemoryBalloon(&self, num_bytes: i64) -> binder::Result<()> {
Jiyong Park2227eaa2023-08-04 11:59:18 +09001439 self.instance
Keir Fraser48221ba2024-07-12 14:05:02 +00001440 .set_memory_balloon(num_bytes.try_into().unwrap())
1441 .with_context(|| format!("Error setting balloon for VM with CID {}", self.instance.cid))
Jiyong Park2227eaa2023-08-04 11:59:18 +09001442 .with_log()
1443 .or_service_specific_exception(-1)
Keir Frasercdd4b112022-11-24 14:02:25 +00001444 }
1445
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001446 fn connectVsock(&self, port: i32) -> binder::Result<ParcelFileDescriptor> {
Andrew Walbranf8d94112021-09-07 11:45:36 +00001447 if !matches!(&*self.instance.vm_state.lock().unwrap(), VmState::Running { .. }) {
Devin Moore407df8f2024-08-27 19:39:16 +00001448 return Err(Status::new_service_specific_error_str(
1449 IVirtualMachine::ERROR_UNEXPECTED,
1450 Some("Virtual Machine is not running"),
1451 ));
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001452 }
Alan Stokes10c47672022-12-13 17:17:08 +00001453 let port = port as u32;
Devin Moore407df8f2024-08-27 19:39:16 +00001454 if port < VSOCK_PRIV_PORT_MAX {
1455 return Err(Status::new_service_specific_error_str(
1456 IVirtualMachine::ERROR_UNEXPECTED,
1457 Some("Can't connect to privileged port {port}"),
1458 ));
Alan Stokes10c47672022-12-13 17:17:08 +00001459 }
Jiyong Park2227eaa2023-08-04 11:59:18 +09001460 let stream = VsockStream::connect_with_cid_port(self.instance.cid, port)
1461 .context("Failed to connect")
Devin Moore407df8f2024-08-27 19:39:16 +00001462 .or_service_specific_exception(IVirtualMachine::ERROR_UNEXPECTED)?;
Jiyong Parkaf63d1c2024-09-04 16:15:42 +09001463 Ok(vsock_stream_to_pfd(stream))
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001464 }
Yi-Yo Chiang2fbf0da2024-06-14 22:56:56 +08001465
Devin Moore407df8f2024-08-27 19:39:16 +00001466 fn createAccessorBinder(&self, name: &str, port: i32) -> binder::Result<SpIBinder> {
1467 if !matches!(&*self.instance.vm_state.lock().unwrap(), VmState::Running { .. }) {
1468 return Err(Status::new_service_specific_error_str(
1469 IVirtualMachine::ERROR_UNEXPECTED,
1470 Some("Virtual Machine is not running"),
1471 ));
1472 }
1473 let port = port as u32;
1474 if port < VSOCK_PRIV_PORT_MAX {
1475 return Err(Status::new_service_specific_error_str(
1476 IVirtualMachine::ERROR_UNEXPECTED,
1477 Some("Can't connect to privileged port {port}"),
1478 ));
1479 }
1480 let cid = self.instance.cid;
Devin Moorec8800a12024-10-17 17:56:18 +00001481 let addr = sockaddr_vm {
1482 svm_family: AF_VSOCK as sa_family_t,
1483 svm_reserved1: 0,
1484 svm_port: port,
1485 svm_cid: cid,
1486 svm_zero: [0u8; 4],
1487 };
1488 let get_connection_info = move |_instance: &str| Some(ConnectionInfo::Vsock(addr));
Devin Moore407df8f2024-08-27 19:39:16 +00001489 let accessor = Accessor::new(name, get_connection_info);
1490 accessor
1491 .as_binder()
1492 .context("The newly created Accessor should always have a binder")
1493 .or_service_specific_exception(IVirtualMachine::ERROR_UNEXPECTED)
1494 }
1495
Yi-Yo Chiang2fbf0da2024-06-14 22:56:56 +08001496 fn setHostConsoleName(&self, ptsname: &str) -> binder::Result<()> {
1497 self.instance.vm_context.global_context.setHostConsoleName(ptsname)
1498 }
Jiyong Park23b67392024-07-04 13:51:42 +09001499
1500 fn suspend(&self) -> binder::Result<()> {
1501 self.instance
1502 .suspend()
1503 .with_context(|| format!("Error suspending VM with CID {}", self.instance.cid))
1504 .with_log()
1505 .or_service_specific_exception(-1)
1506 }
1507
1508 fn resume(&self) -> binder::Result<()> {
1509 self.instance
1510 .resume()
1511 .with_context(|| format!("Error resuming VM with CID {}", self.instance.cid))
1512 .with_log()
1513 .or_service_specific_exception(-1)
1514 }
Andrew Walbrandae07162021-03-12 17:05:20 +00001515}
1516
1517impl Drop for VirtualMachine {
1518 fn drop(&mut self) {
1519 debug!("Dropping {:?}", self);
Inseob Kima446f802022-07-11 19:46:37 +09001520 if let Err(e) = self.instance.kill() {
1521 debug!("Error stopping dropped VM with CID {}: {:?}", self.instance.cid, e);
1522 }
Andrew Walbrandae07162021-03-12 17:05:20 +00001523 }
1524}
1525
1526/// A set of Binders to be called back in response to various events on the VM, such as when it
1527/// dies.
1528#[derive(Debug, Default)]
1529pub struct VirtualMachineCallbacks(Mutex<Vec<Strong<dyn IVirtualMachineCallback>>>);
1530
1531impl VirtualMachineCallbacks {
Jiyong Park8611a6c2021-07-09 18:17:44 +09001532 /// Call all registered callbacks to notify that the payload has started.
David Brazdil451cc962022-10-14 14:08:12 +01001533 pub fn notify_payload_started(&self, cid: Cid) {
Jiyong Park8611a6c2021-07-09 18:17:44 +09001534 let callbacks = &*self.0.lock().unwrap();
Jiyong Park8611a6c2021-07-09 18:17:44 +09001535 for callback in callbacks {
David Brazdil451cc962022-10-14 14:08:12 +01001536 if let Err(e) = callback.onPayloadStarted(cid as i32) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001537 error!("Error notifying payload start event from VM CID {}: {:?}", cid, e);
Jiyong Park8611a6c2021-07-09 18:17:44 +09001538 }
1539 }
1540 }
1541
Inseob Kim14cb8692021-08-31 21:50:39 +09001542 /// Call all registered callbacks to notify that the payload is ready to serve.
1543 pub fn notify_payload_ready(&self, cid: Cid) {
1544 let callbacks = &*self.0.lock().unwrap();
1545 for callback in callbacks {
1546 if let Err(e) = callback.onPayloadReady(cid as i32) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001547 error!("Error notifying payload ready event from VM CID {}: {:?}", cid, e);
Inseob Kim14cb8692021-08-31 21:50:39 +09001548 }
1549 }
1550 }
1551
Inseob Kim2444af92021-08-31 01:22:50 +09001552 /// Call all registered callbacks to notify that the payload has finished.
1553 pub fn notify_payload_finished(&self, cid: Cid, exit_code: i32) {
1554 let callbacks = &*self.0.lock().unwrap();
1555 for callback in callbacks {
1556 if let Err(e) = callback.onPayloadFinished(cid as i32, exit_code) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001557 error!("Error notifying payload finish event from VM CID {}: {:?}", cid, e);
Inseob Kim2444af92021-08-31 01:22:50 +09001558 }
1559 }
1560 }
1561
Jooyung Handd0a1732021-11-23 15:26:20 +09001562 /// Call all registered callbacks to say that the VM encountered an error.
Alan Stokes2bead0d2022-09-05 16:58:34 +01001563 pub fn notify_error(&self, cid: Cid, error_code: ErrorCode, message: &str) {
Jooyung Handd0a1732021-11-23 15:26:20 +09001564 let callbacks = &*self.0.lock().unwrap();
1565 for callback in callbacks {
1566 if let Err(e) = callback.onError(cid as i32, error_code, message) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001567 error!("Error notifying error event from VM CID {}: {:?}", cid, e);
Jooyung Handd0a1732021-11-23 15:26:20 +09001568 }
1569 }
1570 }
1571
Andrew Walbrandae07162021-03-12 17:05:20 +00001572 /// Call all registered callbacks to say that the VM has died.
Andrew Walbranc92d35f2022-01-12 12:45:19 +00001573 pub fn callback_on_died(&self, cid: Cid, reason: DeathReason) {
Andrew Walbrandae07162021-03-12 17:05:20 +00001574 let callbacks = &*self.0.lock().unwrap();
1575 for callback in callbacks {
Andrew Walbranc92d35f2022-01-12 12:45:19 +00001576 if let Err(e) = callback.onDied(cid as i32, reason) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001577 error!("Error notifying exit of VM CID {}: {:?}", cid, e);
Andrew Walbrandae07162021-03-12 17:05:20 +00001578 }
1579 }
1580 }
1581
1582 /// Add a new callback to the set.
1583 fn add(&self, callback: Strong<dyn IVirtualMachineCallback>) {
1584 self.0.lock().unwrap().push(callback);
1585 }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001586}
1587
Andrew Walbranf6bf6862021-05-21 12:41:13 +00001588/// The mutable state of the VirtualizationService. There should only be one instance of this
1589/// struct.
Chris Wailes641fc4a2021-12-01 15:03:21 -08001590#[derive(Debug, Default)]
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001591struct State {
Alan Stokes3e5eec12023-09-07 12:10:00 +01001592 /// The VMs which have been started. When VMs are started a weak reference is added to this
1593 /// list while a strong reference is returned to the caller over Binder. Once all copies of
1594 /// the Binder client are dropped the weak reference here will become invalid, and will be
1595 /// removed from the list opportunistically the next time `add_vm` is called.
Andrew Walbran320b5602021-03-04 16:11:12 +00001596 vms: Vec<Weak<VmInstance>>,
1597}
1598
1599impl State {
Andrew Walbrandae07162021-03-12 17:05:20 +00001600 /// Get a list of VMs which still have Binder references to them.
Andrew Walbran320b5602021-03-04 16:11:12 +00001601 fn vms(&self) -> Vec<Arc<VmInstance>> {
1602 // Attempt to upgrade the weak pointers to strong pointers.
1603 self.vms.iter().filter_map(Weak::upgrade).collect()
1604 }
1605
1606 /// Add a new VM to the list.
1607 fn add_vm(&mut self, vm: Weak<VmInstance>) {
1608 // Garbage collect any entries from the stored list which no longer exist.
1609 self.vms.retain(|vm| vm.strong_count() > 0);
1610
1611 // Actually add the new VM.
1612 self.vms.push(vm);
1613 }
David Brazdil3c2ddef2021-03-18 13:09:57 +00001614
Jiyong Park8611a6c2021-07-09 18:17:44 +09001615 /// Get a VM that corresponds to the given cid
1616 fn get_vm(&self, cid: Cid) -> Option<Arc<VmInstance>> {
1617 self.vms().into_iter().find(|vm| vm.cid == cid)
1618 }
Jiyong Parkd50a0242021-09-16 21:00:14 +09001619}
1620
Andrew Walbran6b650662021-09-07 13:13:23 +00001621/// Gets the `VirtualMachineState` of the given `VmInstance`.
1622fn get_state(instance: &VmInstance) -> VirtualMachineState {
Andrew Walbranf8d94112021-09-07 11:45:36 +00001623 match &*instance.vm_state.lock().unwrap() {
1624 VmState::NotStarted { .. } => VirtualMachineState::NOT_STARTED,
1625 VmState::Running { .. } => match instance.payload_state() {
Andrew Walbran6b650662021-09-07 13:13:23 +00001626 PayloadState::Starting => VirtualMachineState::STARTING,
1627 PayloadState::Started => VirtualMachineState::STARTED,
1628 PayloadState::Ready => VirtualMachineState::READY,
1629 PayloadState::Finished => VirtualMachineState::FINISHED,
Jiyong Parka4eebde2022-07-12 18:01:12 +09001630 PayloadState::Hangup => VirtualMachineState::DEAD,
Andrew Walbranf8d94112021-09-07 11:45:36 +00001631 },
1632 VmState::Dead => VirtualMachineState::DEAD,
1633 VmState::Failed => VirtualMachineState::DEAD,
Andrew Walbran6b650662021-09-07 13:13:23 +00001634 }
1635}
1636
David Brazdilf50c7a62023-04-19 14:22:42 +00001637/// Converts a `&ParcelFileDescriptor` to a `File` by cloning the file.
Jiyong Park2227eaa2023-08-04 11:59:18 +09001638pub fn clone_file(file: &ParcelFileDescriptor) -> binder::Result<File> {
1639 file.as_ref()
1640 .try_clone()
1641 .context("Failed to clone File from ParcelFileDescriptor")
1642 .or_binder_exception(ExceptionCode::BAD_PARCELABLE)
Andrei Homescu11333c62023-11-09 04:26:39 +00001643 .map(File::from)
David Brazdilf50c7a62023-04-19 14:22:42 +00001644}
1645
Andrew Walbrand3a84182021-09-07 14:48:52 +00001646/// Converts an `&Option<ParcelFileDescriptor>` to an `Option<File>` by cloning the file.
Jiyong Park2227eaa2023-08-04 11:59:18 +09001647fn maybe_clone_file(file: &Option<ParcelFileDescriptor>) -> binder::Result<Option<File>> {
Andrew Walbrand3a84182021-09-07 14:48:52 +00001648 file.as_ref().map(clone_file).transpose()
1649}
1650
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001651/// Converts a `VsockStream` to a `ParcelFileDescriptor`.
Jiyong Parkaf63d1c2024-09-04 16:15:42 +09001652fn vsock_stream_to_pfd(stream: VsockStream) -> ParcelFileDescriptor {
1653 // SAFETY: ownership is transferred from stream to f
1654 let f = unsafe { File::from_raw_fd(stream.into_raw_fd()) };
1655 ParcelFileDescriptor::new(f)
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001656}
1657
Jiyong Parkdcf17412022-02-08 15:07:23 +09001658/// Parses the platform version requirement string.
Jiyong Park2227eaa2023-08-04 11:59:18 +09001659fn parse_platform_version_req(s: &str) -> binder::Result<VersionReq> {
1660 VersionReq::parse(s)
1661 .with_context(|| format!("Invalid platform version requirement {}", s))
1662 .or_binder_exception(ExceptionCode::BAD_PARCELABLE)
Jiyong Parkdcf17412022-02-08 15:07:23 +09001663}
1664
Jiyong Parked180932023-02-24 19:55:41 +09001665/// Create the empty ramdump file
1666fn prepare_ramdump_file(temporary_directory: &Path) -> binder::Result<File> {
1667 // `ramdump_write` is sent to crosvm and will be the backing store for the /dev/hvc1 where
1668 // VM will emit ramdump to. `ramdump_read` will be sent back to the client (i.e. the VM
1669 // owner) for readout.
1670 let ramdump_path = temporary_directory.join("ramdump");
Jiyong Park2227eaa2023-08-04 11:59:18 +09001671 let ramdump = File::create(ramdump_path)
1672 .context("Failed to prepare ramdump file")
1673 .with_log()
1674 .or_service_specific_exception(-1)?;
Jiyong Parked180932023-02-24 19:55:41 +09001675 Ok(ramdump)
1676}
1677
Pierre-Clément Tosi9515d0f2024-04-15 15:58:07 +01001678/// Create the empty device tree dump file
1679fn prepare_dump_dt_file(temporary_directory: &Path) -> binder::Result<File> {
1680 let path = temporary_directory.join("device_tree.dtb");
1681 let file = File::create(path)
1682 .context("Failed to prepare device tree dump file")
1683 .with_log()
1684 .or_service_specific_exception(-1)?;
1685 Ok(file)
1686}
1687
Nikita Ioffe5776f082023-02-10 21:38:26 +00001688fn is_protected(config: &VirtualMachineConfig) -> bool {
1689 match config {
1690 VirtualMachineConfig::RawConfig(config) => config.protectedVm,
1691 VirtualMachineConfig::AppConfig(config) => config.protectedVm,
1692 }
1693}
1694
1695fn check_gdb_allowed(config: &VirtualMachineConfig) -> binder::Result<()> {
1696 if is_protected(config) {
Jiyong Park2227eaa2023-08-04 11:59:18 +09001697 return Err(anyhow!("Can't use gdb with protected VMs"))
1698 .or_binder_exception(ExceptionCode::SECURITY);
Nikita Ioffe5776f082023-02-10 21:38:26 +00001699 }
1700
Pierre-Clément Tosid3bbe1d2024-04-15 18:03:51 +01001701 if get_debug_level(config) == Some(DebugLevel::NONE) {
1702 return Err(anyhow!("Can't use gdb with non-debuggable VMs"))
1703 .or_binder_exception(ExceptionCode::SECURITY);
Nikita Ioffe5776f082023-02-10 21:38:26 +00001704 }
Pierre-Clément Tosid3bbe1d2024-04-15 18:03:51 +01001705
1706 Ok(())
Nikita Ioffe5776f082023-02-10 21:38:26 +00001707}
1708
Shikha Panwar61a74b52024-02-16 13:17:01 +00001709fn extract_instance_id(config: &VirtualMachineConfig) -> [u8; 64] {
1710 match config {
1711 VirtualMachineConfig::RawConfig(config) => config.instanceId,
1712 VirtualMachineConfig::AppConfig(config) => config.instanceId,
1713 }
1714}
1715
Alan Stokesc96b35e2024-05-03 13:21:20 +01001716fn extract_want_updatable(config: &VirtualMachineConfig) -> bool {
1717 match config {
1718 VirtualMachineConfig::RawConfig(_) => true,
1719 VirtualMachineConfig::AppConfig(config) => {
1720 let Some(custom) = &config.customConfig else { return true };
1721 custom.wantUpdatable
1722 }
1723 }
1724}
1725
Nikita Ioffe5776f082023-02-10 21:38:26 +00001726fn extract_gdb_port(config: &VirtualMachineConfig) -> Option<NonZeroU16> {
1727 match config {
1728 VirtualMachineConfig::RawConfig(config) => NonZeroU16::new(config.gdbPort as u16),
Nikita Ioffea0eb5ee2023-06-26 18:18:21 +01001729 VirtualMachineConfig::AppConfig(config) => {
1730 NonZeroU16::new(config.customConfig.as_ref().map(|c| c.gdbPort).unwrap_or(0) as u16)
1731 }
Nikita Ioffe5776f082023-02-10 21:38:26 +00001732 }
1733}
1734
Nikita Ioffe631717e2023-09-05 13:38:07 +01001735fn check_no_vendor_modules(config: &VirtualMachineConfig) -> binder::Result<()> {
1736 let VirtualMachineConfig::AppConfig(config) = config else { return Ok(()) };
1737 if let Some(custom_config) = &config.customConfig {
1738 if custom_config.vendorImage.is_some() || custom_config.customKernelImage.is_some() {
1739 return Err(anyhow!("vendor modules feature is disabled"))
1740 .or_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION);
1741 }
1742 }
1743 Ok(())
1744}
1745
Nikita Ioffe94a8a182023-11-16 16:37:48 +00001746fn check_no_devices(config: &VirtualMachineConfig) -> binder::Result<()> {
1747 let VirtualMachineConfig::AppConfig(config) = config else { return Ok(()) };
1748 if let Some(custom_config) = &config.customConfig {
1749 if !custom_config.devices.is_empty() {
1750 return Err(anyhow!("device assignment feature is disabled"))
1751 .or_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION);
1752 }
1753 }
1754 Ok(())
1755}
1756
Alan Stokesfda70842023-12-20 17:50:14 +00001757fn check_no_extra_apks(config: &VirtualMachineConfig) -> binder::Result<()> {
1758 let VirtualMachineConfig::AppConfig(config) = config else { return Ok(()) };
1759 let Payload::PayloadConfig(payload_config) = &config.payload else { return Ok(()) };
1760 if !payload_config.extraApks.is_empty() {
1761 return Err(anyhow!("multi-tenant feature is disabled"))
1762 .or_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION);
1763 }
1764 Ok(())
1765}
1766
Nikita Ioffeb4268b32024-09-03 10:23:14 +00001767fn check_no_extra_kernel_cmdline_params(config: &VirtualMachineConfig) -> binder::Result<()> {
1768 let VirtualMachineConfig::AppConfig(config) = config else { return Ok(()) };
1769 if let Some(custom_config) = &config.customConfig {
1770 if !custom_config.extraKernelCmdlineParams.is_empty() {
1771 return Err(anyhow!("debuggable_vms_improvements feature is disabled"))
1772 .or_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION);
1773 }
1774 }
1775 Ok(())
1776}
1777
Nikita Ioffef934e812024-07-05 15:44:41 +00001778fn check_protected_vm_is_supported() -> binder::Result<()> {
1779 let is_pvm_supported =
1780 hypervisor_props::is_protected_vm_supported().or_service_specific_exception(-1)?;
1781 if is_pvm_supported {
1782 Ok(())
1783 } else {
1784 Err(anyhow!("pVM is not supported"))
1785 .or_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION)
1786 }
1787}
1788
Nikita Ioffe631717e2023-09-05 13:38:07 +01001789fn check_config_features(config: &VirtualMachineConfig) -> binder::Result<()> {
1790 if !cfg!(vendor_modules) {
1791 check_no_vendor_modules(config)?;
1792 }
Nikita Ioffe94a8a182023-11-16 16:37:48 +00001793 if !cfg!(device_assignment) {
1794 check_no_devices(config)?;
1795 }
Alan Stokesfda70842023-12-20 17:50:14 +00001796 if !cfg!(multi_tenant) {
1797 check_no_extra_apks(config)?;
1798 }
Nikita Ioffeb4268b32024-09-03 10:23:14 +00001799 if !cfg!(debuggable_vms_improvements) {
1800 check_no_extra_kernel_cmdline_params(config)?;
1801 }
Nikita Ioffe631717e2023-09-05 13:38:07 +01001802 Ok(())
1803}
1804
Inseob Kimecde8c02024-09-03 13:11:08 +09001805fn check_config_allowed_for_early_vms(config: &VirtualMachineConfig) -> binder::Result<()> {
1806 check_no_vendor_modules(config)?;
1807 check_no_devices(config)?;
1808
1809 Ok(())
1810}
1811
Inseob Kim0168b462022-12-27 14:54:35 +09001812fn clone_or_prepare_logger_fd(
Inseob Kim0168b462022-12-27 14:54:35 +09001813 fd: Option<&ParcelFileDescriptor>,
1814 tag: String,
1815) -> Result<Option<File>, Status> {
1816 if let Some(fd) = fd {
1817 return Ok(Some(clone_file(fd)?));
1818 }
1819
Frederick Maylefbbcfcd2024-04-08 16:31:54 -07001820 let (read_fd, write_fd) =
Jiyong Park2227eaa2023-08-04 11:59:18 +09001821 pipe().context("Failed to create pipe").or_service_specific_exception(-1)?;
Inseob Kim0168b462022-12-27 14:54:35 +09001822
Frederick Maylefbbcfcd2024-04-08 16:31:54 -07001823 let mut reader = BufReader::new(File::from(read_fd));
1824 let write_fd = File::from(write_fd);
Inseob Kim0168b462022-12-27 14:54:35 +09001825
1826 std::thread::spawn(move || loop {
1827 let mut buf = vec![];
1828 match reader.read_until(b'\n', &mut buf) {
1829 Ok(0) => {
1830 // EOF
1831 return;
1832 }
1833 Ok(size) => {
1834 if buf[size - 1] == b'\n' {
1835 buf.pop();
1836 }
1837 info!("{}: {}", &tag, &String::from_utf8_lossy(&buf));
1838 }
1839 Err(e) => {
1840 error!("Could not read console pipe: {:?}", e);
1841 return;
1842 }
1843 };
1844 });
1845
1846 Ok(Some(write_fd))
1847}
1848
Jooyung Han35edb8f2021-07-01 16:17:16 +09001849/// Simple utility for referencing Borrowed or Owned. Similar to std::borrow::Cow, but
1850/// it doesn't require that T implements Clone.
1851enum BorrowedOrOwned<'a, T> {
1852 Borrowed(&'a T),
1853 Owned(T),
1854}
1855
1856impl<'a, T> AsRef<T> for BorrowedOrOwned<'a, T> {
1857 fn as_ref(&self) -> &T {
1858 match self {
1859 Self::Borrowed(b) => b,
Chris Wailes68c39f82021-07-27 16:03:44 -07001860 Self::Owned(o) => o,
Jooyung Han35edb8f2021-07-01 16:17:16 +09001861 }
1862 }
1863}
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001864
1865/// Implementation of `IVirtualMachineService`, the entry point of the AIDL service.
1866#[derive(Debug, Default)]
1867struct VirtualMachineService {
1868 state: Arc<Mutex<State>>,
Inseob Kimc7d28c72021-10-25 14:28:10 +00001869 cid: Cid,
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001870}
1871
1872impl Interface for VirtualMachineService {}
1873
1874impl IVirtualMachineService for VirtualMachineService {
Inseob Kimc7d28c72021-10-25 14:28:10 +00001875 fn notifyPayloadStarted(&self) -> binder::Result<()> {
1876 let cid = self.cid;
Inseob Kim7f61fe72021-08-20 20:50:47 +09001877 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001878 info!("VM with CID {} started payload", cid);
Jiyong Park2227eaa2023-08-04 11:59:18 +09001879 vm.update_payload_state(PayloadState::Started)
1880 .or_binder_exception(ExceptionCode::ILLEGAL_STATE)?;
David Brazdil451cc962022-10-14 14:08:12 +01001881 vm.callbacks.notify_payload_started(cid);
Seungjae Yoo62085c02022-08-12 04:44:52 +00001882
Seungjae Yoo6d265d92022-11-15 10:51:33 +09001883 let vm_start_timestamp = vm.vm_metric.lock().unwrap().start_timestamp;
1884 write_vm_booted_stats(vm.requester_uid as i32, &vm.name, vm_start_timestamp);
Inseob Kim7f61fe72021-08-20 20:50:47 +09001885 Ok(())
1886 } else {
Jooyung Handd0a1732021-11-23 15:26:20 +09001887 error!("notifyPayloadStarted is called from an unknown CID {}", cid);
Jiyong Park2227eaa2023-08-04 11:59:18 +09001888 Err(anyhow!("cannot find a VM with CID {}", cid)).or_service_specific_exception(-1)
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001889 }
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001890 }
Inseob Kim2444af92021-08-31 01:22:50 +09001891
Inseob Kimc7d28c72021-10-25 14:28:10 +00001892 fn notifyPayloadReady(&self) -> binder::Result<()> {
1893 let cid = self.cid;
Inseob Kim14cb8692021-08-31 21:50:39 +09001894 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001895 info!("VM with CID {} reported payload is ready", cid);
Jiyong Park2227eaa2023-08-04 11:59:18 +09001896 vm.update_payload_state(PayloadState::Ready)
1897 .or_binder_exception(ExceptionCode::ILLEGAL_STATE)?;
Inseob Kim14cb8692021-08-31 21:50:39 +09001898 vm.callbacks.notify_payload_ready(cid);
1899 Ok(())
1900 } else {
Jooyung Handd0a1732021-11-23 15:26:20 +09001901 error!("notifyPayloadReady is called from an unknown CID {}", cid);
Jiyong Park2227eaa2023-08-04 11:59:18 +09001902 Err(anyhow!("cannot find a VM with CID {}", cid)).or_service_specific_exception(-1)
Inseob Kim14cb8692021-08-31 21:50:39 +09001903 }
1904 }
1905
Inseob Kimc7d28c72021-10-25 14:28:10 +00001906 fn notifyPayloadFinished(&self, exit_code: i32) -> binder::Result<()> {
1907 let cid = self.cid;
Inseob Kim2444af92021-08-31 01:22:50 +09001908 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001909 info!("VM with CID {} finished payload", cid);
Jiyong Park2227eaa2023-08-04 11:59:18 +09001910 vm.update_payload_state(PayloadState::Finished)
1911 .or_binder_exception(ExceptionCode::ILLEGAL_STATE)?;
Inseob Kim2444af92021-08-31 01:22:50 +09001912 vm.callbacks.notify_payload_finished(cid, exit_code);
1913 Ok(())
1914 } else {
Jooyung Handd0a1732021-11-23 15:26:20 +09001915 error!("notifyPayloadFinished is called from an unknown CID {}", cid);
Jiyong Park2227eaa2023-08-04 11:59:18 +09001916 Err(anyhow!("cannot find a VM with CID {}", cid)).or_service_specific_exception(-1)
Jooyung Handd0a1732021-11-23 15:26:20 +09001917 }
1918 }
1919
Alan Stokes2bead0d2022-09-05 16:58:34 +01001920 fn notifyError(&self, error_code: ErrorCode, message: &str) -> binder::Result<()> {
Jooyung Handd0a1732021-11-23 15:26:20 +09001921 let cid = self.cid;
1922 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001923 info!("VM with CID {} encountered an error", cid);
Jiyong Park2227eaa2023-08-04 11:59:18 +09001924 vm.update_payload_state(PayloadState::Finished)
1925 .or_binder_exception(ExceptionCode::ILLEGAL_STATE)?;
Jooyung Handd0a1732021-11-23 15:26:20 +09001926 vm.callbacks.notify_error(cid, error_code, message);
1927 Ok(())
1928 } else {
Seungjae Yooec8c1602022-06-20 05:28:00 +00001929 error!("notifyError is called from an unknown CID {}", cid);
Jiyong Park2227eaa2023-08-04 11:59:18 +09001930 Err(anyhow!("cannot find a VM with CID {}", cid)).or_service_specific_exception(-1)
Inseob Kim2444af92021-08-31 01:22:50 +09001931 }
1932 }
Alice Wangc2fec932023-02-23 16:24:02 +00001933
Shikha Panware45e9422024-02-28 21:18:10 +00001934 fn getSecretkeeper(&self) -> binder::Result<Strong<dyn ISecretkeeper>> {
1935 if !is_secretkeeper_supported() {
1936 return Err(StatusCode::NAME_NOT_FOUND)?;
1937 }
1938 let sk = binder::wait_for_interface(SECRETKEEPER_IDENTIFIER)?;
1939 Ok(BnSecretkeeper::new_binder(SecretkeeperProxy(sk), BinderFeatures::default()))
Shikha Panwar5d6a6752023-12-14 22:08:26 +00001940 }
1941
Alice Wange64dd182024-01-17 15:57:55 +00001942 fn requestAttestation(&self, csr: &[u8], test_mode: bool) -> binder::Result<Vec<Certificate>> {
1943 GLOBAL_SERVICE.requestAttestation(csr, get_calling_uid() as i32, test_mode)
Alice Wangc2fec932023-02-23 16:24:02 +00001944 }
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001945}
1946
Shikha Panwar8707f0f2024-02-28 20:40:42 +00001947fn is_secretkeeper_supported() -> bool {
Shikha Panwar81d13d32024-03-10 19:39:23 +00001948 binder::is_declared(SECRETKEEPER_IDENTIFIER)
1949 .expect("Could not check for declared Secretkeeper interface")
Shikha Panwar8187ca22024-01-04 08:33:08 +00001950}
1951
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001952impl VirtualMachineService {
Inseob Kimc7d28c72021-10-25 14:28:10 +00001953 fn new_binder(state: Arc<Mutex<State>>, cid: Cid) -> Strong<dyn IVirtualMachineService> {
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001954 BnVirtualMachineService::new_binder(
Inseob Kimc7d28c72021-10-25 14:28:10 +00001955 VirtualMachineService { state, cid },
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001956 BinderFeatures::default(),
1957 )
1958 }
1959}
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001960
Stephen Hines4c1e2fc2024-02-09 16:34:47 -08001961struct SecretkeeperProxy(Strong<dyn ISecretkeeper>);
1962
1963impl Interface for SecretkeeperProxy {}
1964
1965impl ISecretkeeper for SecretkeeperProxy {
1966 fn processSecretManagementRequest(&self, req: &[u8]) -> binder::Result<Vec<u8>> {
1967 // Pass the request to the channel, and read the response.
1968 self.0.processSecretManagementRequest(req)
1969 }
1970
1971 fn getAuthGraphKe(&self) -> binder::Result<Strong<dyn IAuthGraphKeyExchange>> {
1972 let ag = AuthGraphKeyExchangeProxy(self.0.getAuthGraphKe()?);
1973 Ok(BnAuthGraphKeyExchange::new_binder(ag, BinderFeatures::default()))
1974 }
1975
1976 fn deleteIds(&self, ids: &[SecretId]) -> binder::Result<()> {
1977 self.0.deleteIds(ids)
1978 }
1979
1980 fn deleteAll(&self) -> binder::Result<()> {
1981 self.0.deleteAll()
1982 }
1983}
1984
1985struct AuthGraphKeyExchangeProxy(Strong<dyn IAuthGraphKeyExchange>);
1986
1987impl Interface for AuthGraphKeyExchangeProxy {}
1988
1989impl IAuthGraphKeyExchange for AuthGraphKeyExchangeProxy {
1990 fn create(&self) -> binder::Result<SessionInitiationInfo> {
1991 self.0.create()
1992 }
1993
1994 fn init(
1995 &self,
1996 peer_pub_key: &PubKey,
1997 peer_id: &Identity,
1998 peer_nonce: &[u8],
1999 peer_version: i32,
2000 ) -> binder::Result<KeInitResult> {
2001 self.0.init(peer_pub_key, peer_id, peer_nonce, peer_version)
2002 }
2003
2004 fn finish(
2005 &self,
2006 peer_pub_key: &PubKey,
2007 peer_id: &Identity,
2008 peer_signature: &SessionIdSignature,
2009 peer_nonce: &[u8],
2010 peer_version: i32,
2011 own_key: &Key,
2012 ) -> binder::Result<SessionInfo> {
2013 self.0.finish(peer_pub_key, peer_id, peer_signature, peer_nonce, peer_version, own_key)
2014 }
2015
2016 fn authenticationComplete(
2017 &self,
2018 peer_signature: &SessionIdSignature,
2019 shared_keys: &[AuthgraphArc; 2],
2020 ) -> binder::Result<[AuthgraphArc; 2]> {
2021 self.0.authenticationComplete(peer_signature, shared_keys)
2022 }
2023}
2024
Inseob Kimecde8c02024-09-03 13:11:08 +09002025// KEEP IN SYNC WITH early_vms.xsd
2026#[derive(Debug, Deserialize, PartialEq)]
2027struct EarlyVm {
2028 #[allow(dead_code)]
2029 name: String,
2030 #[allow(dead_code)]
2031 cid: i32,
2032 #[allow(dead_code)]
2033 path: String,
2034}
2035
2036#[derive(Debug, Default, Deserialize)]
2037struct EarlyVms {
2038 #[allow(dead_code)]
2039 early_vm: Vec<EarlyVm>,
2040}
2041
2042fn range_for_partition(partition: &str) -> Result<Range<Cid>> {
2043 match partition {
2044 "system" => Ok(100..200),
2045 "system_ext" | "product" => Ok(200..300),
2046 _ => Err(anyhow!("Early VMs are not supported for {partition}")),
2047 }
2048}
2049
2050fn find_early_vm(xml_path: &Path, cid_range: &Range<Cid>, name: &str) -> Result<EarlyVm> {
2051 if !xml_path.exists() {
2052 bail!("{} doesn't exist", xml_path.display());
2053 }
2054
2055 let xml =
2056 fs::read(xml_path).with_context(|| format!("Failed to read {}", xml_path.display()))?;
2057 let xml = String::from_utf8(xml)
2058 .with_context(|| format!("{} is not a valid UTF-8 file", xml_path.display()))?;
2059 let early_vms: EarlyVms = serde_xml_rs::from_str(&xml)
2060 .with_context(|| format!("Can't parse {}", xml_path.display()))?;
2061
2062 let mut found_vm: Option<EarlyVm> = None;
2063
2064 for early_vm in early_vms.early_vm {
2065 if early_vm.name != name {
2066 continue;
2067 }
2068
2069 let cid = early_vm
2070 .cid
2071 .try_into()
2072 .with_context(|| format!("Invalid CID value {}", early_vm.cid))?;
2073
2074 if !cid_range.contains(&cid) {
2075 bail!("VM '{}' uses CID {cid} which is out of range. Available CIDs for '{}': {cid_range:?}", xml_path.display(), early_vm.name);
2076 }
2077
2078 if found_vm.is_some() {
2079 bail!("Multiple VMs named {name} are found in {}", xml_path.display());
2080 }
2081
2082 found_vm = Some(early_vm);
2083 }
2084
2085 found_vm.ok_or_else(|| anyhow!("Can't find {name} in {}", xml_path.display()))
2086}
2087
2088fn find_early_vm_for_partition(partition: &str, name: &str) -> Result<EarlyVm> {
2089 let cid_range = range_for_partition(partition)?;
2090 find_early_vm(Path::new(&format!("/{partition}/etc/avf/early_vms.xml")), &cid_range, name)
2091}
2092
Alan Stokes53cc5ca2022-08-30 14:28:19 +01002093#[cfg(test)]
2094mod tests {
2095 use super::*;
2096
2097 #[test]
2098 fn test_is_allowed_label_for_partition() -> Result<()> {
2099 let expected_results = vec![
2100 ("u:object_r:system_file:s0", true),
2101 ("u:object_r:apk_data_file:s0", true),
2102 ("u:object_r:app_data_file:s0", false),
2103 ("u:object_r:app_data_file:s0:c512,c768", false),
2104 ("u:object_r:privapp_data_file:s0:c512,c768", false),
2105 ("invalid", false),
2106 ("user:role:apk_data_file:severity:categories", true),
2107 ("user:role:apk_data_file:severity:categories:extraneous", false),
2108 ];
2109
2110 for (label, expected_valid) in expected_results {
2111 let context = SeContext::new(label)?;
2112 let result = check_label_is_allowed(&context);
2113 if expected_valid {
2114 assert!(result.is_ok(), "Expected label {} to be allowed, got {:?}", label, result);
2115 } else if result.is_ok() {
2116 bail!("Expected label {} to be disallowed", label);
2117 }
2118 }
2119 Ok(())
2120 }
Nikita Ioffef1ce9872022-12-09 13:31:59 +00002121
2122 #[test]
2123 fn test_create_or_update_idsig_file_empty_apk() -> Result<()> {
2124 let apk = tempfile::tempfile().unwrap();
2125 let idsig = tempfile::tempfile().unwrap();
2126
2127 let ret = create_or_update_idsig_file(
2128 &ParcelFileDescriptor::new(apk),
2129 &ParcelFileDescriptor::new(idsig),
2130 );
2131 assert!(ret.is_err(), "should fail");
2132 Ok(())
2133 }
2134
2135 #[test]
2136 fn test_create_or_update_idsig_dir_instead_of_file_for_apk() -> Result<()> {
2137 let tmp_dir = tempfile::TempDir::new().unwrap();
2138 let apk = File::open(tmp_dir.path()).unwrap();
2139 let idsig = tempfile::tempfile().unwrap();
2140
2141 let ret = create_or_update_idsig_file(
2142 &ParcelFileDescriptor::new(apk),
2143 &ParcelFileDescriptor::new(idsig),
2144 );
2145 assert!(ret.is_err(), "should fail");
2146 Ok(())
2147 }
2148
2149 /// Verifies that create_or_update_idsig_file won't oom if a fd that corresponds to a directory
2150 /// on ext4 filesystem is passed.
2151 /// On ext4 lseek on a directory fd will return (off_t)-1 (see:
2152 /// https://bugzilla.kernel.org/show_bug.cgi?id=200043), which will result in
2153 /// create_or_update_idsig_file ooming while attempting to allocate petabytes of memory.
2154 #[test]
2155 fn test_create_or_update_idsig_does_not_crash_dir_on_ext4() -> Result<()> {
2156 // APEXes are backed by the ext4.
2157 let apk = File::open("/apex/com.android.virt/").unwrap();
2158 let idsig = tempfile::tempfile().unwrap();
2159
2160 let ret = create_or_update_idsig_file(
2161 &ParcelFileDescriptor::new(apk),
2162 &ParcelFileDescriptor::new(idsig),
2163 );
2164 assert!(ret.is_err(), "should fail");
2165 Ok(())
2166 }
Jiyong Park8d192952023-06-26 14:29:51 +09002167
2168 #[test]
2169 fn test_create_or_update_idsig_does_not_update_if_already_valid() -> Result<()> {
2170 use std::io::Seek;
2171
2172 // Pick any APK
2173 let mut apk = File::open("/system/priv-app/Shell/Shell.apk").unwrap();
2174 let mut idsig = tempfile::tempfile().unwrap();
2175
2176 create_or_update_idsig_file(
2177 &ParcelFileDescriptor::new(apk.try_clone()?),
2178 &ParcelFileDescriptor::new(idsig.try_clone()?),
2179 )?;
2180 let modified_orig = idsig.metadata()?.modified()?;
2181 apk.rewind()?;
2182 idsig.rewind()?;
2183
2184 // Call the function again
2185 create_or_update_idsig_file(
2186 &ParcelFileDescriptor::new(apk.try_clone()?),
2187 &ParcelFileDescriptor::new(idsig.try_clone()?),
2188 )?;
2189 let modified_new = idsig.metadata()?.modified()?;
2190 assert!(modified_orig == modified_new, "idsig file was updated unnecessarily");
2191 Ok(())
2192 }
Nikita Ioffeaa6858c2023-07-04 01:37:41 +01002193
2194 #[test]
Shikha Panwar9ae2e622024-01-30 12:22:30 +00002195 fn test_create_or_update_idsig_on_non_empty_file() -> Result<()> {
2196 use std::io::Read;
2197
2198 // Pick any APK
2199 let mut apk = File::open("/system/priv-app/Shell/Shell.apk").unwrap();
2200 let idsig_empty = tempfile::tempfile().unwrap();
2201 let mut idsig_invalid = tempfile::tempfile().unwrap();
2202 idsig_invalid.write_all(b"Oops")?;
2203
2204 // Create new idsig
2205 create_or_update_idsig_file(
2206 &ParcelFileDescriptor::new(apk.try_clone()?),
2207 &ParcelFileDescriptor::new(idsig_empty.try_clone()?),
2208 )?;
2209 apk.rewind()?;
2210
2211 // Update idsig_invalid
2212 create_or_update_idsig_file(
2213 &ParcelFileDescriptor::new(apk.try_clone()?),
2214 &ParcelFileDescriptor::new(idsig_invalid.try_clone()?),
2215 )?;
2216
2217 // Ensure the 2 idsig files have same size!
2218 assert!(
2219 idsig_empty.metadata()?.len() == idsig_invalid.metadata()?.len(),
2220 "idsig files differ in size"
2221 );
2222 // Ensure the 2 idsig files have same content!
2223 for (b1, b2) in idsig_empty.bytes().zip(idsig_invalid.bytes()) {
2224 assert!(b1.unwrap() == b2.unwrap(), "idsig files differ")
2225 }
2226 Ok(())
2227 }
2228 #[test]
Nikita Ioffeaa6858c2023-07-04 01:37:41 +01002229 fn test_append_kernel_param_first_param() {
2230 let mut vm_config = VirtualMachineRawConfig { ..Default::default() };
2231 append_kernel_param("foo=1", &mut vm_config);
2232 assert_eq!(vm_config.params, Some("foo=1".to_owned()))
2233 }
2234
2235 #[test]
2236 fn test_append_kernel_param() {
2237 let mut vm_config =
2238 VirtualMachineRawConfig { params: Some("foo=5".to_owned()), ..Default::default() };
2239 append_kernel_param("bar=42", &mut vm_config);
2240 assert_eq!(vm_config.params, Some("foo=5 bar=42".to_owned()))
2241 }
Seungjae Yooec3bc522023-11-09 10:14:30 +09002242
Inseob Kim46257382024-01-03 15:41:22 +09002243 fn test_extract_os_name_from_config_path(
2244 path: &Path,
2245 expected_result: Option<&str>,
2246 ) -> Result<()> {
2247 let result = extract_os_name_from_config_path(path);
2248 if result.as_deref() != expected_result {
2249 bail!("Expected {:?} but was {:?}", expected_result, &result)
2250 }
2251 Ok(())
2252 }
2253
2254 #[test]
2255 fn test_extract_os_name_from_microdroid_config() -> Result<()> {
2256 test_extract_os_name_from_config_path(
2257 Path::new("/apex/com.android.virt/etc/microdroid.json"),
2258 Some("microdroid"),
2259 )
2260 }
2261
2262 #[test]
Nikita Ioffef49350e2024-11-01 16:11:48 +00002263 fn test_extract_os_name_from_microdroid_16k_config() -> Result<()> {
2264 test_extract_os_name_from_config_path(
2265 Path::new("/apex/com.android.virt/etc/microdroid_16k.json"),
2266 Some("microdroid_16k"),
2267 )
2268 }
2269
2270 #[test]
Inseob Kim46257382024-01-03 15:41:22 +09002271 fn test_extract_os_name_from_microdroid_gki_config() -> Result<()> {
2272 test_extract_os_name_from_config_path(
2273 Path::new("/apex/com.android.virt/etc/microdroid_gki-android14-6.1.json"),
2274 Some("microdroid_gki-android14-6.1"),
2275 )
2276 }
2277
2278 #[test]
2279 fn test_extract_os_name_from_invalid_path() -> Result<()> {
2280 test_extract_os_name_from_config_path(
2281 Path::new("/apex/com.android.virt/etc/microdroid.img"),
2282 None,
2283 )
2284 }
2285
2286 #[test]
2287 fn test_extract_os_name_from_configs() -> Result<()> {
2288 let tmp_dir = tempfile::TempDir::new()?;
2289 let tmp_dir_path = tmp_dir.path().to_owned();
2290
2291 let mut os_names: HashSet<String> = HashSet::new();
2292 os_names.insert("microdroid".to_owned());
2293 os_names.insert("microdroid_gki-android14-6.1".to_owned());
2294 os_names.insert("microdroid_gki-android15-6.1".to_owned());
2295
2296 // config files
2297 for os_name in &os_names {
2298 std::fs::write(tmp_dir_path.join(os_name.to_owned() + ".json"), b"")?;
2299 }
2300
2301 // fake files not related to configs
2302 std::fs::write(tmp_dir_path.join("microdroid_super.img"), b"")?;
2303 std::fs::write(tmp_dir_path.join("microdroid_foobar.apk"), b"")?;
2304
2305 let glob_pattern = match tmp_dir_path.join("microdroid*.json").to_str() {
2306 Some(s) => s.to_owned(),
2307 None => bail!("tmp_dir_path {:?} is not UTF-8", tmp_dir_path),
2308 };
2309
2310 let result = extract_os_names_from_configs(&glob_pattern)?;
2311 if result != os_names {
2312 bail!("Expected {:?} but was {:?}", os_names, result);
2313 }
2314 Ok(())
2315 }
Inseob Kimecde8c02024-09-03 13:11:08 +09002316
2317 #[test]
2318 fn test_find_early_vms_from_xml() -> Result<()> {
2319 let tmp_dir = tempfile::TempDir::new()?;
2320 let tmp_dir_path = tmp_dir.path().to_owned();
2321 let xml_path = tmp_dir_path.join("early_vms.xml");
2322
2323 std::fs::write(
2324 &xml_path,
2325 br#"<?xml version="1.0" encoding="utf-8"?>
2326 <early_vms>
2327 <early_vm>
2328 <name>vm_demo_native_early</name>
2329 <cid>123</cid>
2330 <path>/system/bin/vm_demo_native_early</path>
2331 </early_vm>
2332 <early_vm>
2333 <name>vm_demo_duplicated_name</name>
2334 <cid>456</cid>
2335 <path>/system/bin/vm_demo_duplicated_name_1</path>
2336 </early_vm>
2337 <early_vm>
2338 <name>vm_demo_duplicated_name</name>
2339 <cid>789</cid>
2340 <path>/system/bin/vm_demo_duplicated_name_2</path>
2341 </early_vm>
2342 <early_vm>
2343 <name>vm_demo_invalid_cid_1</name>
2344 <cid>-1</cid>
2345 <path>/system/bin/vm_demo_invalid_cid_1</path>
2346 </early_vm>
2347 <early_vm>
2348 <name>vm_demo_invalid_cid_2</name>
2349 <cid>999999</cid>
2350 <path>/system/bin/vm_demo_invalid_cid_2</path>
2351 </early_vm>
2352 </early_vms>
2353 "#,
2354 )?;
2355
2356 let cid_range = 100..1000;
2357
2358 let result = find_early_vm(&xml_path, &cid_range, "vm_demo_native_early")?;
2359 let expected = EarlyVm {
2360 name: "vm_demo_native_early".to_owned(),
2361 cid: 123,
2362 path: "/system/bin/vm_demo_native_early".to_owned(),
2363 };
2364 assert_eq!(result, expected);
2365
2366 assert!(
2367 find_early_vm(&xml_path, &cid_range, "vm_demo_duplicated_name").is_err(),
2368 "should fail"
2369 );
2370 assert!(
2371 find_early_vm(&xml_path, &cid_range, "vm_demo_invalid_cid_1").is_err(),
2372 "should fail"
2373 );
2374 assert!(
2375 find_early_vm(&xml_path, &cid_range, "vm_demo_invalid_cid_2").is_err(),
2376 "should fail"
2377 );
2378
2379 Ok(())
2380 }
Alan Stokes53cc5ca2022-08-30 14:28:19 +01002381}