blob: e968830f58e1180a7a89bb4d8b95874d46736304 [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
David Brazdil1f530702022-10-03 12:18:10 +010017use crate::{get_calling_pid, get_calling_uid};
David Brazdil49f96f52022-12-16 21:29:13 +000018use crate::atom::{
19 forward_vm_booted_atom, forward_vm_creation_atom, forward_vm_exited_atom,
20 write_vm_booted_stats, write_vm_creation_stats};
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000021use crate::composite::make_composite_image;
David Brazdil8cf8f482022-11-23 14:21:26 +000022use crate::crosvm::{CrosvmConfig, DiskFile, PayloadState, VmContext, VmInstance, VmState};
Shikha Panwar22e70452022-10-10 18:32:55 +000023use crate::payload::{add_microdroid_payload_images, add_microdroid_system_images};
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::{
Andrew Walbranc92d35f2022-01-12 12:45:19 +000027 DeathReason::DeathReason,
David Brazdil49f96f52022-12-16 21:29:13 +000028 ErrorCode::ErrorCode,
29};
30use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{
Andrew Walbran6b650662021-09-07 13:13:23 +000031 DiskImage::DiskImage,
Alan Stokes0cc59ee2021-09-24 11:20:34 +010032 IVirtualMachine::{BnVirtualMachine, IVirtualMachine},
Andrew Walbran6b650662021-09-07 13:13:23 +000033 IVirtualMachineCallback::IVirtualMachineCallback,
34 IVirtualizationService::IVirtualizationService,
Keir Frasercdd4b112022-11-24 14:02:25 +000035 MemoryTrimLevel::MemoryTrimLevel,
Jiyong Park029977d2021-11-24 21:56:49 +090036 Partition::Partition,
Andrew Walbran6b650662021-09-07 13:13:23 +000037 PartitionType::PartitionType,
Inseob Kim0168b462022-12-27 14:54:35 +090038 VirtualMachineAppConfig::{DebugLevel::DebugLevel, Payload::Payload, VirtualMachineAppConfig},
Jooyung Han21e9b922021-06-26 04:14:16 +090039 VirtualMachineConfig::VirtualMachineConfig,
Andrew Walbran6b650662021-09-07 13:13:23 +000040 VirtualMachineDebugInfo::VirtualMachineDebugInfo,
Alan Stokes0d1ef782022-09-27 13:46:35 +010041 VirtualMachinePayloadConfig::VirtualMachinePayloadConfig,
Jooyung Han21e9b922021-06-26 04:14:16 +090042 VirtualMachineRawConfig::VirtualMachineRawConfig,
Andrew Walbran6b650662021-09-07 13:13:23 +000043 VirtualMachineState::VirtualMachineState,
Jooyung Han21e9b922021-06-26 04:14:16 +090044};
David Brazdil528e0472022-10-10 15:06:02 +010045use android_system_virtualizationservice_internal::aidl::android::system::virtualizationservice_internal::{
David Brazdil49f96f52022-12-16 21:29:13 +000046 AtomVmBooted::AtomVmBooted,
47 AtomVmCreationRequested::AtomVmCreationRequested,
48 AtomVmExited::AtomVmExited,
David Brazdil528e0472022-10-10 15:06:02 +010049 IGlobalVmContext::{BnGlobalVmContext, IGlobalVmContext},
David Brazdil4b4c5102022-12-19 22:56:20 +000050 IVirtualizationServiceInternal::IVirtualizationServiceInternal,
David Brazdil528e0472022-10-10 15:06:02 +010051};
Seungjae Yoodd91f0f2022-11-09 15:25:21 +090052use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::{
David Brazdil73988ea2022-11-11 15:10:32 +000053 BnVirtualMachineService, IVirtualMachineService, VM_TOMBSTONES_SERVICE_PORT,
Seungjae Yoofd9a0622022-10-14 10:01:29 +090054};
55use anyhow::{anyhow, bail, Context, Result};
56use apkverify::{HashAlgorithm, V4Signature};
Alan Stokes0e82b502022-08-08 14:44:48 +010057use binder::{
David Brazdil4b4c5102022-12-19 22:56:20 +000058 self, wait_for_interface, BinderFeatures, ExceptionCode, Interface, LazyServiceGuard,
59 ParcelFileDescriptor, Status, StatusCode, Strong,
Andrew Walbrana89fc132021-03-17 17:08:36 +000060};
Andrew Walbrandfc953d2021-06-10 13:59:56 +000061use disk::QcowFile;
David Brazdil49f96f52022-12-16 21:29:13 +000062use lazy_static::lazy_static;
David Brazdila07a1792022-10-25 13:37:57 +010063use libc::VMADDR_CID_HOST;
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +000064use log::{debug, error, info, warn};
Seungjae Yoofd9a0622022-10-14 10:01:29 +090065use microdroid_payload_config::{OsConfig, Task, TaskType, VmPayloadConfig};
Inseob Kim0168b462022-12-27 14:54:35 +090066use nix::unistd::pipe;
David Brazdil73988ea2022-11-11 15:10:32 +000067use rpcbinder::RpcServer;
Jiyong Parkd50a0242021-09-16 21:00:14 +090068use rustutils::system_properties;
Jiyong Parkdcf17412022-02-08 15:07:23 +090069use semver::VersionReq;
David Brazdil73988ea2022-11-11 15:10:32 +000070use std::collections::HashMap;
Andrew Walbrandff3b942021-06-09 15:20:36 +000071use std::convert::TryInto;
Shikha Panward8e35422021-10-11 13:51:27 +000072use std::ffi::CStr;
David Brazdil4b4c5102022-12-19 22:56:20 +000073use std::fs::{create_dir, read_dir, remove_dir, remove_file, set_permissions, File, OpenOptions, Permissions};
Inseob Kim0168b462022-12-27 14:54:35 +090074use std::io::{BufRead, BufReader, Error, ErrorKind, Read, Write};
Andrew Walbranb15cd6e2021-07-05 16:38:07 +000075use std::num::NonZeroU32;
David Brazdil4b4c5102022-12-19 22:56:20 +000076use std::os::unix::fs::PermissionsExt;
Andrew Walbrand3a84182021-09-07 14:48:52 +000077use std::os::unix::io::{FromRawFd, IntoRawFd};
David Brazdild4f51a52023-01-11 14:09:27 +000078use std::os::unix::raw::{pid_t, uid_t};
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000079use std::path::{Path, PathBuf};
Andrew Walbran320b5602021-03-04 16:11:12 +000080use std::sync::{Arc, Mutex, Weak};
Seungjae Yoofd9a0622022-10-14 10:01:29 +090081use tombstoned_client::{DebuggerdDumpType, TombstonedConnection};
Andrew Walbrancc0db522021-07-12 17:03:42 +000082use vmconfig::VmConfig;
Andrew Walbranadd38cb2022-10-06 17:01:03 +000083use vsock::{VsockListener, VsockStream};
Jooyung Han35edb8f2021-07-01 16:17:16 +090084use zip::ZipArchive;
David Brazdil4b4c5102022-12-19 22:56:20 +000085use nix::unistd::{chown, Uid};
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000086
David Brazdil41d1a872022-10-05 14:44:19 +010087/// The unique ID of a VM used (together with a port number) for vsock communication.
88pub type Cid = u32;
89
David Brazdil4b4c5102022-12-19 22:56:20 +000090pub const BINDER_SERVICE_IDENTIFIER: &str = "android.system.virtualizationservice";
91
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000092/// Directory in which to write disk image files used while running VMs.
Andrew Walbran488bd072021-07-14 13:29:51 +000093pub const TEMPORARY_DIRECTORY: &str = "/data/misc/virtualizationservice";
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000094
David Brazdil41d1a872022-10-05 14:44:19 +010095/// The first CID to assign to a guest VM managed by the VirtualizationService. CIDs lower than this
96/// are reserved for the host or other usage.
David Brazdil73988ea2022-11-11 15:10:32 +000097const GUEST_CID_MIN: Cid = 2048;
98const GUEST_CID_MAX: Cid = 65535;
David Brazdil41d1a872022-10-05 14:44:19 +010099
100const SYSPROP_LAST_CID: &str = "virtualizationservice.state.last_cid";
Jiyong Park8611a6c2021-07-09 18:17:44 +0900101
Jooyung Han95884632021-07-06 22:27:54 +0900102/// The size of zero.img.
103/// Gaps in composite disk images are filled with a shared zero.img.
104const ZERO_FILLER_SIZE: u64 = 4096;
105
Jiyong Park9dd389e2021-08-23 20:42:59 +0900106/// Magic string for the instance image
107const ANDROID_VM_INSTANCE_MAGIC: &str = "Android-VM-instance";
108
109/// Version of the instance image format
110const ANDROID_VM_INSTANCE_VERSION: u16 = 1;
111
Shikha Panwar7afc1392022-03-24 08:54:43 +0000112const CHUNK_RECV_MAX_LEN: usize = 1024;
113
Alan Stokes0d1ef782022-09-27 13:46:35 +0100114const MICRODROID_OS_NAME: &str = "microdroid";
115
Shikha Panwar9fd198f2022-11-18 17:43:43 +0000116const UNFORMATTED_STORAGE_MAGIC: &str = "UNFORMATTED-STORAGE";
117
David Brazdil49f96f52022-12-16 21:29:13 +0000118lazy_static! {
David Brazdil4b4c5102022-12-19 22:56:20 +0000119 pub static ref GLOBAL_SERVICE: Strong<dyn IVirtualizationServiceInternal> =
120 wait_for_interface(BINDER_SERVICE_IDENTIFIER)
121 .expect("Could not connect to VirtualizationServiceInternal");
David Brazdil49f96f52022-12-16 21:29:13 +0000122}
123
David Brazdil73988ea2022-11-11 15:10:32 +0000124fn is_valid_guest_cid(cid: Cid) -> bool {
125 (GUEST_CID_MIN..=GUEST_CID_MAX).contains(&cid)
126}
127
Nikita Ioffef1ce9872022-12-09 13:31:59 +0000128fn create_or_update_idsig_file(
129 input_fd: &ParcelFileDescriptor,
130 idsig_fd: &ParcelFileDescriptor,
131) -> Result<()> {
132 let mut input = clone_file(input_fd)?;
133 let metadata = input.metadata().context("failed to get input metadata")?;
134 if !metadata.is_file() {
135 bail!("input is not a regular file");
136 }
137 let mut sig = V4Signature::create(&mut input, 4096, &[], HashAlgorithm::SHA256)
138 .context("failed to create idsig")?;
139
140 let mut output = clone_file(idsig_fd)?;
Nikita Ioffec09b0492022-12-14 20:18:33 +0000141 output.set_len(0).context("failed to set_len on the idsig output")?;
142 sig.write_into(&mut output).context("failed to write idsig")?;
Nikita Ioffef1ce9872022-12-09 13:31:59 +0000143 Ok(())
144}
145
David Brazdil528e0472022-10-10 15:06:02 +0100146/// Singleton service for allocating globally-unique VM resources, such as the CID, and running
147/// singleton servers, like tombstone receiver.
Jooyung Han9900f3d2021-07-06 10:27:54 +0900148#[derive(Debug, Default)]
David Brazdil528e0472022-10-10 15:06:02 +0100149pub struct VirtualizationServiceInternal {
150 state: Arc<Mutex<GlobalState>>,
151}
152
153impl VirtualizationServiceInternal {
David Brazdil4b4c5102022-12-19 22:56:20 +0000154 // TODO(b/245727626): Remove after the source files for virtualizationservice
155 // and virtmgr binaries are split from each other.
156 #[allow(dead_code)]
David Brazdil528e0472022-10-10 15:06:02 +0100157 pub fn init() -> VirtualizationServiceInternal {
158 let service = VirtualizationServiceInternal::default();
159
160 std::thread::spawn(|| {
161 if let Err(e) = handle_stream_connection_tombstoned() {
162 warn!("Error receiving tombstone from guest or writing them. Error: {:?}", e);
163 }
164 });
165
166 service
167 }
168}
169
170impl Interface for VirtualizationServiceInternal {}
171
172impl IVirtualizationServiceInternal for VirtualizationServiceInternal {
David Brazdil4b4c5102022-12-19 22:56:20 +0000173 fn removeMemlockRlimit(&self) -> binder::Result<()> {
174 let pid = get_calling_pid();
175 let lim = libc::rlimit { rlim_cur: libc::RLIM_INFINITY, rlim_max: libc::RLIM_INFINITY };
176
177 // SAFETY - borrowing the new limit struct only
178 let ret = unsafe { libc::prlimit(pid, libc::RLIMIT_MEMLOCK, &lim, std::ptr::null_mut()) };
179
180 match ret {
181 0 => Ok(()),
182 -1 => Err(Status::new_exception_str(
183 ExceptionCode::ILLEGAL_STATE,
184 Some(std::io::Error::last_os_error().to_string()),
185 )),
186 n => Err(Status::new_exception_str(
187 ExceptionCode::ILLEGAL_STATE,
188 Some(format!("Unexpected return value from prlimit(): {n}")),
189 )),
190 }
191 }
192
David Brazdild4f51a52023-01-11 14:09:27 +0000193 fn allocateGlobalVmContext(
194 &self,
195 requester_debug_pid: i32,
196 ) -> binder::Result<Strong<dyn IGlobalVmContext>> {
David Brazdil31c22e62023-01-11 13:52:27 +0000197 let requester_uid = get_calling_uid();
David Brazdild4f51a52023-01-11 14:09:27 +0000198 let requester_debug_pid = requester_debug_pid as pid_t;
David Brazdil528e0472022-10-10 15:06:02 +0100199 let state = &mut *self.state.lock().unwrap();
David Brazdild4f51a52023-01-11 14:09:27 +0000200 state.allocate_vm_context(requester_uid, requester_debug_pid).map_err(|e| {
David Brazdil528e0472022-10-10 15:06:02 +0100201 Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
David Brazdil4b4c5102022-12-19 22:56:20 +0000202 })
David Brazdil528e0472022-10-10 15:06:02 +0100203 }
David Brazdil49f96f52022-12-16 21:29:13 +0000204
205 fn atomVmBooted(&self, atom: &AtomVmBooted) -> Result<(), Status> {
206 forward_vm_booted_atom(atom);
207 Ok(())
208 }
209
210 fn atomVmCreationRequested(&self, atom: &AtomVmCreationRequested) -> Result<(), Status> {
211 forward_vm_creation_atom(atom);
212 Ok(())
213 }
214
215 fn atomVmExited(&self, atom: &AtomVmExited) -> Result<(), Status> {
216 forward_vm_exited_atom(atom);
217 Ok(())
218 }
David Brazdild4f51a52023-01-11 14:09:27 +0000219
220 fn debugListVms(&self) -> binder::Result<Vec<VirtualMachineDebugInfo>> {
221 let state = &mut *self.state.lock().unwrap();
222 let cids = state
223 .held_contexts
224 .iter()
225 .filter_map(|(_, inst)| Weak::upgrade(inst))
226 .map(|vm| VirtualMachineDebugInfo {
227 cid: vm.cid as i32,
228 temporaryDirectory: vm.get_temp_dir().to_string_lossy().to_string(),
229 requesterUid: vm.requester_uid as i32,
230 requesterPid: vm.requester_debug_pid as i32,
231 })
232 .collect();
233 Ok(cids)
234 }
David Brazdil528e0472022-10-10 15:06:02 +0100235}
236
David Brazdil31c22e62023-01-11 13:52:27 +0000237#[derive(Debug, Default)]
238struct GlobalVmInstance {
239 /// The unique CID assigned to the VM for vsock communication.
240 cid: Cid,
David Brazdild4f51a52023-01-11 14:09:27 +0000241 /// UID of the client who requested this VM instance.
242 requester_uid: uid_t,
243 /// PID of the client who requested this VM instance.
244 requester_debug_pid: pid_t,
David Brazdil31c22e62023-01-11 13:52:27 +0000245}
246
247impl GlobalVmInstance {
248 fn get_temp_dir(&self) -> PathBuf {
249 let cid = self.cid;
250 format!("{TEMPORARY_DIRECTORY}/{cid}").into()
251 }
David Brazdil528e0472022-10-10 15:06:02 +0100252}
253
254/// The mutable state of the VirtualizationServiceInternal. There should only be one instance
255/// of this struct.
256#[derive(Debug, Default)]
David Brazdil73988ea2022-11-11 15:10:32 +0000257struct GlobalState {
David Brazdil31c22e62023-01-11 13:52:27 +0000258 /// VM contexts currently allocated to running VMs. A CID is never recycled as long
David Brazdil73988ea2022-11-11 15:10:32 +0000259 /// as there is a strong reference held by a GlobalVmContext.
David Brazdil31c22e62023-01-11 13:52:27 +0000260 held_contexts: HashMap<Cid, Weak<GlobalVmInstance>>,
David Brazdil73988ea2022-11-11 15:10:32 +0000261}
David Brazdil528e0472022-10-10 15:06:02 +0100262
263impl GlobalState {
264 /// Get the next available CID, or an error if we have run out. The last CID used is stored in
265 /// a system property so that restart of virtualizationservice doesn't reuse CID while the host
266 /// Android is up.
David Brazdil31c22e62023-01-11 13:52:27 +0000267 fn get_next_available_cid(&mut self) -> Result<Cid> {
David Brazdil73988ea2022-11-11 15:10:32 +0000268 // Start trying to find a CID from the last used CID + 1. This ensures
David Brazdil8cf8f482022-11-23 14:21:26 +0000269 // that we do not eagerly recycle CIDs. It makes debugging easier but
270 // also means that retrying to allocate a CID, eg. because it is
271 // erroneously occupied by a process, will not recycle the same CID.
David Brazdil73988ea2022-11-11 15:10:32 +0000272 let last_cid_prop =
273 system_properties::read(SYSPROP_LAST_CID)?.and_then(|val| match val.parse::<Cid>() {
274 Ok(num) => {
275 if is_valid_guest_cid(num) {
276 Some(num)
277 } else {
278 error!("Invalid value '{}' of property '{}'", num, SYSPROP_LAST_CID);
279 None
280 }
281 }
David Brazdil528e0472022-10-10 15:06:02 +0100282 Err(_) => {
283 error!("Invalid value '{}' of property '{}'", val, SYSPROP_LAST_CID);
David Brazdil73988ea2022-11-11 15:10:32 +0000284 None
David Brazdil528e0472022-10-10 15:06:02 +0100285 }
David Brazdil73988ea2022-11-11 15:10:32 +0000286 });
287
288 let first_cid = if let Some(last_cid) = last_cid_prop {
David Brazdil31c22e62023-01-11 13:52:27 +0000289 if last_cid == GUEST_CID_MAX {
290 GUEST_CID_MIN
291 } else {
292 last_cid + 1
293 }
David Brazdil73988ea2022-11-11 15:10:32 +0000294 } else {
295 GUEST_CID_MIN
David Brazdil528e0472022-10-10 15:06:02 +0100296 };
David Brazdil73988ea2022-11-11 15:10:32 +0000297
298 let cid = self
299 .find_available_cid(first_cid..=GUEST_CID_MAX)
David Brazdil31c22e62023-01-11 13:52:27 +0000300 .or_else(|| self.find_available_cid(GUEST_CID_MIN..first_cid))
301 .ok_or_else(|| anyhow!("Could not find an available CID."))?;
David Brazdil73988ea2022-11-11 15:10:32 +0000302
David Brazdil31c22e62023-01-11 13:52:27 +0000303 system_properties::write(SYSPROP_LAST_CID, &format!("{}", cid))?;
304 Ok(cid)
David Brazdil73988ea2022-11-11 15:10:32 +0000305 }
306
307 fn find_available_cid<I>(&self, mut range: I) -> Option<Cid>
308 where
309 I: Iterator<Item = Cid>,
310 {
David Brazdil31c22e62023-01-11 13:52:27 +0000311 range.find(|cid| !self.held_contexts.contains_key(cid))
David Brazdil528e0472022-10-10 15:06:02 +0100312 }
David Brazdil4b4c5102022-12-19 22:56:20 +0000313
David Brazdil31c22e62023-01-11 13:52:27 +0000314 fn allocate_vm_context(
315 &mut self,
316 requester_uid: uid_t,
David Brazdild4f51a52023-01-11 14:09:27 +0000317 requester_debug_pid: pid_t,
David Brazdil31c22e62023-01-11 13:52:27 +0000318 ) -> Result<Strong<dyn IGlobalVmContext>> {
319 // Garbage collect unused VM contexts.
320 self.held_contexts.retain(|_, instance| instance.strong_count() > 0);
321
322 let cid = self.get_next_available_cid()?;
David Brazdild4f51a52023-01-11 14:09:27 +0000323 let instance = Arc::new(GlobalVmInstance { cid, requester_uid, requester_debug_pid });
David Brazdil31c22e62023-01-11 13:52:27 +0000324 create_temporary_directory(&instance.get_temp_dir(), requester_uid)?;
325
326 self.held_contexts.insert(cid, Arc::downgrade(&instance));
327 let binder = GlobalVmContext { instance, ..Default::default() };
David Brazdil4b4c5102022-12-19 22:56:20 +0000328 Ok(BnGlobalVmContext::new_binder(binder, BinderFeatures::default()))
329 }
330}
331
David Brazdil31c22e62023-01-11 13:52:27 +0000332fn create_temporary_directory(path: &PathBuf, requester_uid: uid_t) -> Result<()> {
David Brazdil4b4c5102022-12-19 22:56:20 +0000333 if path.as_path().exists() {
David Brazdil31c22e62023-01-11 13:52:27 +0000334 remove_temporary_dir(path).unwrap_or_else(|e| {
David Brazdil4b4c5102022-12-19 22:56:20 +0000335 warn!("Could not delete temporary directory {:?}: {}", path, e);
336 });
337 }
338 // Create a directory that is owned by client's UID but system's GID, and permissions 0700.
339 // If the chown() fails, this will leave behind an empty directory that will get removed
340 // at the next attempt, or if virtualizationservice is restarted.
David Brazdil31c22e62023-01-11 13:52:27 +0000341 create_dir(path).with_context(|| format!("Could not create temporary directory {:?}", path))?;
342 chown(path, Some(Uid::from_raw(requester_uid)), None)
David Brazdil4b4c5102022-12-19 22:56:20 +0000343 .with_context(|| format!("Could not set ownership of temporary directory {:?}", path))?;
David Brazdil31c22e62023-01-11 13:52:27 +0000344 Ok(())
David Brazdil4b4c5102022-12-19 22:56:20 +0000345}
346
347/// Removes a directory owned by a different user by first changing its owner back
348/// to VirtualizationService.
349pub fn remove_temporary_dir(path: &PathBuf) -> Result<()> {
350 if !path.as_path().is_dir() {
351 bail!("Path {:?} is not a directory", path);
352 }
353 chown(path, Some(Uid::current()), None)?;
354 set_permissions(path, Permissions::from_mode(0o700))?;
355 remove_temporary_files(path)?;
356 remove_dir(path)?;
357 Ok(())
358}
359
360pub fn remove_temporary_files(path: &PathBuf) -> Result<()> {
361 for dir_entry in read_dir(path)? {
362 remove_file(dir_entry?.path())?;
363 }
364 Ok(())
David Brazdil528e0472022-10-10 15:06:02 +0100365}
366
367/// Implementation of the AIDL `IGlobalVmContext` interface.
368#[derive(Debug, Default)]
369struct GlobalVmContext {
David Brazdil31c22e62023-01-11 13:52:27 +0000370 /// Strong reference to the context's instance data structure.
371 instance: Arc<GlobalVmInstance>,
David Brazdil73988ea2022-11-11 15:10:32 +0000372 /// Keeps our service process running as long as this VM context exists.
David Brazdil528e0472022-10-10 15:06:02 +0100373 #[allow(dead_code)]
374 lazy_service_guard: LazyServiceGuard,
375}
376
David Brazdil528e0472022-10-10 15:06:02 +0100377impl Interface for GlobalVmContext {}
378
379impl IGlobalVmContext for GlobalVmContext {
380 fn getCid(&self) -> binder::Result<i32> {
David Brazdil31c22e62023-01-11 13:52:27 +0000381 Ok(self.instance.cid as i32)
David Brazdil528e0472022-10-10 15:06:02 +0100382 }
David Brazdil4b4c5102022-12-19 22:56:20 +0000383
384 fn getTemporaryDirectory(&self) -> binder::Result<String> {
David Brazdil31c22e62023-01-11 13:52:27 +0000385 Ok(self.instance.get_temp_dir().to_string_lossy().to_string())
David Brazdil4b4c5102022-12-19 22:56:20 +0000386 }
David Brazdil528e0472022-10-10 15:06:02 +0100387}
388
389/// Implementation of `IVirtualizationService`, the entry point of the AIDL service.
David Brazdil49f96f52022-12-16 21:29:13 +0000390#[derive(Debug, Default)]
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000391pub struct VirtualizationService {
Jiyong Park8611a6c2021-07-09 18:17:44 +0900392 state: Arc<Mutex<State>>,
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000393}
394
Shikha Panward8e35422021-10-11 13:51:27 +0000395impl Interface for VirtualizationService {
396 fn dump(&self, mut file: &File, _args: &[&CStr]) -> Result<(), StatusCode> {
397 check_permission("android.permission.DUMP").or(Err(StatusCode::PERMISSION_DENIED))?;
398 let state = &mut *self.state.lock().unwrap();
399 let vms = state.vms();
400 writeln!(file, "Running {0} VMs:", vms.len()).or(Err(StatusCode::UNKNOWN_ERROR))?;
401 for vm in vms {
402 writeln!(file, "VM CID: {}", vm.cid).or(Err(StatusCode::UNKNOWN_ERROR))?;
403 writeln!(file, "\tState: {:?}", vm.vm_state.lock().unwrap())
404 .or(Err(StatusCode::UNKNOWN_ERROR))?;
405 writeln!(file, "\tPayload state {:?}", vm.payload_state())
406 .or(Err(StatusCode::UNKNOWN_ERROR))?;
407 writeln!(file, "\tProtected: {}", vm.protected).or(Err(StatusCode::UNKNOWN_ERROR))?;
408 writeln!(file, "\ttemporary_directory: {}", vm.temporary_directory.to_string_lossy())
409 .or(Err(StatusCode::UNKNOWN_ERROR))?;
410 writeln!(file, "\trequester_uid: {}", vm.requester_uid)
411 .or(Err(StatusCode::UNKNOWN_ERROR))?;
Shikha Panward8e35422021-10-11 13:51:27 +0000412 writeln!(file, "\trequester_debug_pid: {}", vm.requester_debug_pid)
413 .or(Err(StatusCode::UNKNOWN_ERROR))?;
414 }
415 Ok(())
416 }
417}
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000418
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000419impl IVirtualizationService for VirtualizationService {
Andrew Walbranf8d94112021-09-07 11:45:36 +0000420 /// Creates (but does not start) a new VM with the given configuration, assigning it the next
421 /// available CID.
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000422 ///
423 /// Returns a binder `IVirtualMachine` object referring to it, as a handle for the client.
Andrew Walbranf8d94112021-09-07 11:45:36 +0000424 fn createVm(
Andrew Walbrana89fc132021-03-17 17:08:36 +0000425 &self,
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000426 config: &VirtualMachineConfig,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900427 console_fd: Option<&ParcelFileDescriptor>,
Andrew Walbrana89fc132021-03-17 17:08:36 +0000428 log_fd: Option<&ParcelFileDescriptor>,
429 ) -> binder::Result<Strong<dyn IVirtualMachine>> {
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000430 let mut is_protected = false;
431 let ret = self.create_vm_internal(config, console_fd, log_fd, &mut is_protected);
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +0000432 write_vm_creation_stats(config, is_protected, &ret);
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000433 ret
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000434 }
Andrew Walbran320b5602021-03-04 16:11:12 +0000435
Andrew Walbrandff3b942021-06-09 15:20:36 +0000436 /// Initialise an empty partition image of the given size to be used as a writable partition.
437 fn initializeWritablePartition(
438 &self,
439 image_fd: &ParcelFileDescriptor,
440 size: i64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900441 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000442 ) -> binder::Result<()> {
Jiyong Park753553b2021-07-12 21:21:09 +0900443 check_manage_access()?;
Andrew Walbrandfc953d2021-06-10 13:59:56 +0000444 let size = size.try_into().map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000445 Status::new_exception_str(
Andrew Walbran806f1542021-06-10 14:07:12 +0000446 ExceptionCode::ILLEGAL_ARGUMENT,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100447 Some(format!("Invalid size {}: {:?}", size, e)),
Andrew Walbran806f1542021-06-10 14:07:12 +0000448 )
Andrew Walbrandff3b942021-06-09 15:20:36 +0000449 })?;
Andrew Walbrandfc953d2021-06-10 13:59:56 +0000450 let image = clone_file(image_fd)?;
Jooyung Han1edd5b92021-10-28 10:58:05 +0900451 // initialize the file. Any data in the file will be erased.
452 image.set_len(0).map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000453 Status::new_service_specific_error_str(
454 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100455 Some(format!("Failed to reset a file: {:?}", e)),
Jooyung Han1edd5b92021-10-28 10:58:05 +0900456 )
457 })?;
Jiyong Park9dd389e2021-08-23 20:42:59 +0900458 let mut part = QcowFile::new(image, size).map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000459 Status::new_service_specific_error_str(
460 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100461 Some(format!("Failed to create QCOW2 image: {:?}", e)),
Andrew Walbran806f1542021-06-10 14:07:12 +0000462 )
Andrew Walbrandfc953d2021-06-10 13:59:56 +0000463 })?;
Andrew Walbrandff3b942021-06-09 15:20:36 +0000464
Jiyong Park9dd389e2021-08-23 20:42:59 +0900465 match partition_type {
466 PartitionType::RAW => Ok(()),
467 PartitionType::ANDROID_VM_INSTANCE => format_as_android_vm_instance(&mut part),
Shikha Panwar9fd198f2022-11-18 17:43:43 +0000468 PartitionType::ENCRYPTEDSTORE => format_as_encryptedstore(&mut part),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900469 _ => Err(Error::new(
470 ErrorKind::Unsupported,
471 format!("Unsupported partition type {:?}", partition_type),
472 )),
473 }
474 .map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000475 Status::new_service_specific_error_str(
476 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100477 Some(format!("Failed to initialize partition as {:?}: {:?}", partition_type, e)),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900478 )
479 })?;
480
Andrew Walbrandff3b942021-06-09 15:20:36 +0000481 Ok(())
482 }
483
Jiyong Park0a248432021-08-20 23:32:39 +0900484 /// Creates or update the idsig file by digesting the input APK file.
485 fn createOrUpdateIdsigFile(
486 &self,
487 input_fd: &ParcelFileDescriptor,
488 idsig_fd: &ParcelFileDescriptor,
489 ) -> binder::Result<()> {
490 // TODO(b/193504400): do this only when (1) idsig_fd is empty or (2) the APK digest in
491 // idsig_fd is different from APK digest in input_fd
492
Jiyong Parkc3ca24f2022-06-28 10:45:15 +0900493 check_manage_access()?;
494
Nikita Ioffef1ce9872022-12-09 13:31:59 +0000495 create_or_update_idsig_file(input_fd, idsig_fd)
496 .map_err(|e| Status::new_service_specific_error_str(-1, Some(format!("{:?}", e))))?;
Jiyong Park0a248432021-08-20 23:32:39 +0900497 Ok(())
498 }
499
Andrew Walbran320b5602021-03-04 16:11:12 +0000500 /// Get a list of all currently running VMs. This method is only intended for debug purposes,
501 /// and as such is only permitted from the shell user.
502 fn debugListVms(&self) -> binder::Result<Vec<VirtualMachineDebugInfo>> {
Andrew Walbran806f1542021-06-10 14:07:12 +0000503 check_debug_access()?;
David Brazdild4f51a52023-01-11 14:09:27 +0000504 GLOBAL_SERVICE.debugListVms()
Andrew Walbran320b5602021-03-04 16:11:12 +0000505 }
506}
507
Shikha Panwar7afc1392022-03-24 08:54:43 +0000508fn handle_stream_connection_tombstoned() -> Result<()> {
David Brazdil73988ea2022-11-11 15:10:32 +0000509 // Should not listen for tombstones on a guest VM's port.
510 assert!(!is_valid_guest_cid(VM_TOMBSTONES_SERVICE_PORT as Cid));
Shikha Panwar7afc1392022-03-24 08:54:43 +0000511 let listener =
David Brazdil73988ea2022-11-11 15:10:32 +0000512 VsockListener::bind_with_cid_port(VMADDR_CID_HOST, VM_TOMBSTONES_SERVICE_PORT as Cid)?;
Shikha Panwar7afc1392022-03-24 08:54:43 +0000513 for incoming_stream in listener.incoming() {
514 let mut incoming_stream = match incoming_stream {
515 Err(e) => {
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100516 warn!("invalid incoming connection: {:?}", e);
Shikha Panwar7afc1392022-03-24 08:54:43 +0000517 continue;
518 }
519 Ok(s) => s,
520 };
521 std::thread::spawn(move || {
522 if let Err(e) = handle_tombstone(&mut incoming_stream) {
523 error!("Failed to write tombstone- {:?}", e);
524 }
525 });
526 }
527 Ok(())
528}
529
530fn handle_tombstone(stream: &mut VsockStream) -> Result<()> {
Andrew Walbranadd38cb2022-10-06 17:01:03 +0000531 if let Ok(addr) = stream.peer_addr() {
Shikha Panwar7afc1392022-03-24 08:54:43 +0000532 info!("Vsock Stream connected to cid={} for tombstones", addr.cid());
533 }
534 let tb_connection =
535 TombstonedConnection::connect(std::process::id() as i32, DebuggerdDumpType::Tombstone)
536 .context("Failed to connect to tombstoned")?;
537 let mut text_output = tb_connection
538 .text_output
539 .as_ref()
540 .ok_or_else(|| anyhow!("Could not get file to write the tombstones on"))?;
541 let mut num_bytes_read = 0;
542 loop {
543 let mut chunk_recv = [0; CHUNK_RECV_MAX_LEN];
544 let n = stream
545 .read(&mut chunk_recv)
546 .context("Failed to read tombstone data from Vsock stream")?;
547 if n == 0 {
548 break;
549 }
550 num_bytes_read += n;
551 text_output.write_all(&chunk_recv[0..n]).context("Failed to write guests tombstones")?;
552 }
553 info!("Received {} bytes from guest & wrote to tombstone file", num_bytes_read);
554 tb_connection.notify_completion()?;
555 Ok(())
556}
557
Jiyong Park8611a6c2021-07-09 18:17:44 +0900558impl VirtualizationService {
David Brazdil4b4c5102022-12-19 22:56:20 +0000559 // TODO(b/245727626): Remove after the source files for virtualizationservice
560 // and virtmgr binaries are split from each other.
561 #[allow(dead_code)]
Jiyong Park8611a6c2021-07-09 18:17:44 +0900562 pub fn init() -> VirtualizationService {
David Brazdil49f96f52022-12-16 21:29:13 +0000563 VirtualizationService::default()
Jiyong Park8611a6c2021-07-09 18:17:44 +0900564 }
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000565
David Brazdild4f51a52023-01-11 14:09:27 +0000566 fn create_vm_context(&self, requester_debug_pid: pid_t) -> Result<(VmContext, Cid, PathBuf)> {
David Brazdil8cf8f482022-11-23 14:21:26 +0000567 const NUM_ATTEMPTS: usize = 5;
568
569 for _ in 0..NUM_ATTEMPTS {
David Brazdild4f51a52023-01-11 14:09:27 +0000570 let vm_context = GLOBAL_SERVICE.allocateGlobalVmContext(requester_debug_pid as i32)?;
571 let cid = vm_context.getCid()? as Cid;
572 let temp_dir: PathBuf = vm_context.getTemporaryDirectory()?.into();
David Brazdil8cf8f482022-11-23 14:21:26 +0000573 let service = VirtualMachineService::new_binder(self.state.clone(), cid).as_binder();
574
575 // Start VM service listening for connections from the new CID on port=CID.
David Brazdil8cf8f482022-11-23 14:21:26 +0000576 let port = cid;
David Brazdil3238da42022-11-18 10:04:51 +0000577 match RpcServer::new_vsock(service, cid, port) {
David Brazdil8cf8f482022-11-23 14:21:26 +0000578 Ok(vm_server) => {
579 vm_server.start();
David Brazdild4f51a52023-01-11 14:09:27 +0000580 return Ok((VmContext::new(vm_context, vm_server), cid, temp_dir));
David Brazdil8cf8f482022-11-23 14:21:26 +0000581 }
582 Err(err) => {
583 warn!("Could not start RpcServer on port {}: {}", port, err);
584 }
585 }
586 }
587 bail!("Too many attempts to create VM context failed.");
588 }
589
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000590 fn create_vm_internal(
591 &self,
592 config: &VirtualMachineConfig,
593 console_fd: Option<&ParcelFileDescriptor>,
594 log_fd: Option<&ParcelFileDescriptor>,
595 is_protected: &mut bool,
596 ) -> binder::Result<Strong<dyn IVirtualMachine>> {
597 check_manage_access()?;
Inseob Kim1119d702022-05-02 18:01:58 +0900598
Alan Stokes7bc146c2022-10-20 17:10:32 +0100599 let is_custom = match config {
600 VirtualMachineConfig::RawConfig(_) => true,
601 VirtualMachineConfig::AppConfig(config) => {
602 // Some features are reserved for platform apps only, even when using
603 // VirtualMachineAppConfig:
604 // - controlling CPUs;
605 // - specifying a config file in the APK.
606 !config.taskProfiles.is_empty() || matches!(config.payload, Payload::ConfigPath(_))
Inseob Kim1119d702022-05-02 18:01:58 +0900607 }
Alan Stokes7bc146c2022-10-20 17:10:32 +0100608 };
609 if is_custom {
610 check_use_custom_virtual_machine()?;
Inseob Kim1119d702022-05-02 18:01:58 +0900611 }
612
David Brazdild4f51a52023-01-11 14:09:27 +0000613 let requester_uid = get_calling_uid();
614 let requester_debug_pid = get_calling_pid();
615
616 let (vm_context, cid, temporary_directory) =
617 self.create_vm_context(requester_debug_pid).map_err(|e| {
618 error!("Failed to create VmContext: {:?}", e);
619 Status::new_service_specific_error_str(
620 -1,
621 Some(format!("Failed to create VmContext: {:?}", e)),
622 )
623 })?;
David Brazdil528e0472022-10-10 15:06:02 +0100624
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000625 let state = &mut *self.state.lock().unwrap();
Inseob Kim0168b462022-12-27 14:54:35 +0900626 let console_fd =
627 clone_or_prepare_logger_fd(config, console_fd, format!("Console({})", cid))?;
628 let log_fd = clone_or_prepare_logger_fd(config, log_fd, format!("Log({})", cid))?;
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000629
630 // Counter to generate unique IDs for temporary image files.
631 let mut next_temporary_image_id = 0;
632 // Files which are referred to from composite images. These must be mapped to the crosvm
633 // child process, and not closed before it is started.
634 let mut indirect_files = vec![];
635
Alan Stokes7bc146c2022-10-20 17:10:32 +0100636 let (is_app_config, config) = match config {
637 VirtualMachineConfig::RawConfig(config) => (false, BorrowedOrOwned::Borrowed(config)),
638 VirtualMachineConfig::AppConfig(config) => {
639 let config = load_app_config(config, &temporary_directory).map_err(|e| {
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000640 *is_protected = config.protectedVm;
Alan Stokes0d1ef782022-09-27 13:46:35 +0100641 let message = format!("Failed to load app config: {:?}", e);
642 error!("{}", message);
643 Status::new_service_specific_error_str(-1, Some(message))
Alan Stokes7bc146c2022-10-20 17:10:32 +0100644 })?;
645 (true, BorrowedOrOwned::Owned(config))
646 }
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000647 };
648 let config = config.as_ref();
649 *is_protected = config.protectedVm;
650
651 // Check if partition images are labeled incorrectly. This is to prevent random images
652 // which are not protected by the Android Verified Boot (e.g. bits downloaded by apps) from
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100653 // being loaded in a pVM. This applies to everything in the raw config, and everything but
654 // the non-executable, generated partitions in the app config.
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000655 config
656 .disks
657 .iter()
658 .flat_map(|disk| disk.partitions.iter())
659 .filter(|partition| {
660 if is_app_config {
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100661 !is_safe_app_partition(&partition.label)
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000662 } else {
663 true // all partitions are checked
664 }
665 })
666 .try_for_each(check_label_for_partition)
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100667 .map_err(|e| Status::new_service_specific_error_str(-1, Some(format!("{:?}", e))))?;
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000668
Alan Stokes185fe112023-01-10 16:20:55 +0000669 let kernel = maybe_clone_file(&config.kernel)?;
670 let initrd = maybe_clone_file(&config.initrd)?;
671
672 // In a protected VM, we require custom kernels to come from a trusted source (b/237054515).
673 if config.protectedVm {
674 check_label_for_kernel_files(&kernel, &initrd).map_err(|e| {
675 Status::new_service_specific_error_str(-1, Some(format!("{:?}", e)))
676 })?;
677 }
678
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000679 let zero_filler_path = temporary_directory.join("zero.img");
680 write_zero_filler(&zero_filler_path).map_err(|e| {
Alan Stokes70ccf162022-07-08 11:05:03 +0100681 error!("Failed to make composite image: {:?}", e);
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000682 Status::new_service_specific_error_str(
683 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100684 Some(format!("Failed to make composite image: {:?}", e)),
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000685 )
686 })?;
687
688 // Assemble disk images if needed.
689 let disks = config
690 .disks
691 .iter()
692 .map(|disk| {
693 assemble_disk_image(
694 disk,
695 &zero_filler_path,
696 &temporary_directory,
697 &mut next_temporary_image_id,
698 &mut indirect_files,
699 )
700 })
701 .collect::<Result<Vec<DiskFile>, _>>()?;
702
Jiyong Parke558ab12022-07-07 20:18:55 +0900703 // Creating this ramdump file unconditionally is not harmful as ramdump will be created
704 // only when the VM is configured as such. `ramdump_write` is sent to crosvm and will
Jiyong Park4afe2012022-07-08 05:38:49 +0900705 // be the backing store for the /dev/hvc1 where VM will emit ramdump to. `ramdump_read`
Jiyong Parke558ab12022-07-07 20:18:55 +0900706 // will be sent back to the client (i.e. the VM owner) for readout.
707 let ramdump_path = temporary_directory.join("ramdump");
708 let ramdump = prepare_ramdump_file(&ramdump_path).map_err(|e| {
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100709 error!("Failed to prepare ramdump file: {:?}", e);
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000710 Status::new_service_specific_error_str(
711 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100712 Some(format!("Failed to prepare ramdump file: {:?}", e)),
Jiyong Parke558ab12022-07-07 20:18:55 +0900713 )
714 })?;
715
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000716 // Actually start the VM.
717 let crosvm_config = CrosvmConfig {
718 cid,
Seungjae Yoo62085c02022-08-12 04:44:52 +0000719 name: config.name.clone(),
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000720 bootloader: maybe_clone_file(&config.bootloader)?,
Alan Stokes185fe112023-01-10 16:20:55 +0000721 kernel,
722 initrd,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000723 disks,
724 params: config.params.to_owned(),
725 protected: *is_protected,
726 memory_mib: config.memoryMib.try_into().ok().and_then(NonZeroU32::new),
727 cpus: config.numCpus.try_into().ok().and_then(NonZeroU32::new),
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900728 task_profiles: config.taskProfiles.clone(),
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000729 console_fd,
730 log_fd,
Jiyong Parke558ab12022-07-07 20:18:55 +0900731 ramdump: Some(ramdump),
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000732 indirect_files,
733 platform_version: parse_platform_version_req(&config.platformVersion)?,
Jiyong Parke6ed0f92022-06-22 00:13:00 +0900734 detect_hangup: is_app_config,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000735 };
736 let instance = Arc::new(
David Brazdil528e0472022-10-10 15:06:02 +0100737 VmInstance::new(
738 crosvm_config,
739 temporary_directory,
740 requester_uid,
741 requester_debug_pid,
742 vm_context,
743 )
744 .map_err(|e| {
745 error!("Failed to create VM with config {:?}: {:?}", config, e);
746 Status::new_service_specific_error_str(
747 -1,
748 Some(format!("Failed to create VM: {:?}", e)),
749 )
750 })?,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000751 );
752 state.add_vm(Arc::downgrade(&instance));
753 Ok(VirtualMachine::create(instance))
754 }
Jiyong Park8611a6c2021-07-09 18:17:44 +0900755}
756
Andrew Walbranfbb39d22021-07-28 17:01:25 +0000757fn write_zero_filler(zero_filler_path: &Path) -> Result<()> {
Jooyung Han95884632021-07-06 22:27:54 +0900758 let file = OpenOptions::new()
759 .create_new(true)
760 .read(true)
761 .write(true)
762 .open(zero_filler_path)
763 .with_context(|| "Failed to create zero.img")?;
764 file.set_len(ZERO_FILLER_SIZE)?;
Andrew Walbranfbb39d22021-07-28 17:01:25 +0000765 Ok(())
Jooyung Han95884632021-07-06 22:27:54 +0900766}
767
Jiyong Park9dd389e2021-08-23 20:42:59 +0900768fn format_as_android_vm_instance(part: &mut dyn Write) -> std::io::Result<()> {
769 part.write_all(ANDROID_VM_INSTANCE_MAGIC.as_bytes())?;
770 part.write_all(&ANDROID_VM_INSTANCE_VERSION.to_le_bytes())?;
771 part.flush()
772}
773
Shikha Panwar9fd198f2022-11-18 17:43:43 +0000774fn format_as_encryptedstore(part: &mut dyn Write) -> std::io::Result<()> {
775 part.write_all(UNFORMATTED_STORAGE_MAGIC.as_bytes())?;
776 part.flush()
777}
778
Jiyong Parke558ab12022-07-07 20:18:55 +0900779fn prepare_ramdump_file(ramdump_path: &Path) -> Result<File> {
Chris Wailes9b866f02022-11-16 15:17:16 -0800780 File::create(ramdump_path).context(format!("Failed to create ramdump file {:?}", &ramdump_path))
Jiyong Parke558ab12022-07-07 20:18:55 +0900781}
782
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000783/// Given the configuration for a disk image, assembles the `DiskFile` to pass to crosvm.
784///
785/// This may involve assembling a composite disk from a set of partition images.
786fn assemble_disk_image(
787 disk: &DiskImage,
Jooyung Han95884632021-07-06 22:27:54 +0900788 zero_filler_path: &Path,
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000789 temporary_directory: &Path,
790 next_temporary_image_id: &mut u64,
791 indirect_files: &mut Vec<File>,
Andrew Walbran806f1542021-06-10 14:07:12 +0000792) -> Result<DiskFile, Status> {
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000793 let image = if !disk.partitions.is_empty() {
794 if disk.image.is_some() {
795 warn!("DiskImage {:?} contains both image and partitions.", disk);
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000796 return Err(Status::new_exception_str(
Andrew Walbran806f1542021-06-10 14:07:12 +0000797 ExceptionCode::ILLEGAL_ARGUMENT,
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000798 Some("DiskImage contains both image and partitions."),
Andrew Walbran806f1542021-06-10 14:07:12 +0000799 ));
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000800 }
801
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000802 let composite_image_filenames =
803 make_composite_image_filenames(temporary_directory, next_temporary_image_id);
804 let (image, partition_files) = make_composite_image(
805 &disk.partitions,
Jooyung Han95884632021-07-06 22:27:54 +0900806 zero_filler_path,
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000807 &composite_image_filenames.composite,
808 &composite_image_filenames.header,
809 &composite_image_filenames.footer,
810 )
811 .map_err(|e| {
Alan Stokes70ccf162022-07-08 11:05:03 +0100812 error!("Failed to make composite image with config {:?}: {:?}", disk, e);
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000813 Status::new_service_specific_error_str(
814 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100815 Some(format!("Failed to make composite image: {:?}", e)),
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000816 )
817 })?;
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000818
819 // Pass the file descriptors for the various partition files to crosvm when it
820 // is run.
821 indirect_files.extend(partition_files);
822
823 image
824 } else if let Some(image) = &disk.image {
825 clone_file(image)?
826 } else {
827 warn!("DiskImage {:?} didn't contain image or partitions.", disk);
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000828 return Err(Status::new_exception_str(
Andrew Walbran806f1542021-06-10 14:07:12 +0000829 ExceptionCode::ILLEGAL_ARGUMENT,
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000830 Some("DiskImage didn't contain image or partitions."),
Andrew Walbran806f1542021-06-10 14:07:12 +0000831 ));
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000832 };
833
834 Ok(DiskFile { image, writable: disk.writable })
835}
836
Jooyung Han21e9b922021-06-26 04:14:16 +0900837fn load_app_config(
838 config: &VirtualMachineAppConfig,
839 temporary_directory: &Path,
Jooyung Hanadfb76c2021-06-28 17:29:30 +0900840) -> Result<VirtualMachineRawConfig> {
Andrew Walbrancc0db522021-07-12 17:03:42 +0000841 let apk_file = clone_file(config.apk.as_ref().unwrap())?;
842 let idsig_file = clone_file(config.idsig.as_ref().unwrap())?;
Jiyong Park8d081812021-07-23 17:45:04 +0900843 let instance_file = clone_file(config.instanceImage.as_ref().unwrap())?;
Jooyung Han21e9b922021-06-26 04:14:16 +0900844
Shikha Panwar22e70452022-10-10 18:32:55 +0000845 let storage_image = if let Some(file) = config.encryptedStorageImage.as_ref() {
846 Some(clone_file(file)?)
847 } else {
848 None
849 };
850
Alan Stokes0d1ef782022-09-27 13:46:35 +0100851 let vm_payload_config = match &config.payload {
852 Payload::ConfigPath(config_path) => {
853 load_vm_payload_config_from_file(&apk_file, config_path.as_str())
854 .with_context(|| format!("Couldn't read config from {}", config_path))?
855 }
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000856 Payload::PayloadConfig(payload_config) => create_vm_payload_config(payload_config)?,
Alan Stokes0d1ef782022-09-27 13:46:35 +0100857 };
Jooyung Han21e9b922021-06-26 04:14:16 +0900858
Alan Stokes0d1ef782022-09-27 13:46:35 +0100859 // For now, the only supported OS is Microdroid
860 let os_name = vm_payload_config.os.name.as_str();
861 if os_name != MICRODROID_OS_NAME {
Andrew Walbrancc0db522021-07-12 17:03:42 +0000862 bail!("Unknown OS \"{}\"", os_name);
Jooyung Han35edb8f2021-07-01 16:17:16 +0900863 }
Andrew Walbrancc0db522021-07-12 17:03:42 +0000864
865 // It is safe to construct a filename based on the os_name because we've already checked that it
866 // is one of the allowed values.
Jooyung Han21e9b922021-06-26 04:14:16 +0900867 let vm_config_path = PathBuf::from(format!("/apex/com.android.virt/etc/{}.json", os_name));
868 let vm_config_file = File::open(vm_config_path)?;
Andrew Walbrancc0db522021-07-12 17:03:42 +0000869 let mut vm_config = VmConfig::load(&vm_config_file)?.to_parcelable()?;
Jooyung Han21e9b922021-06-26 04:14:16 +0900870
Andrew Walbrancc045902021-07-27 16:06:17 +0000871 if config.memoryMib > 0 {
872 vm_config.memoryMib = config.memoryMib;
Andrew Walbran45bcb0c2021-07-14 15:02:06 +0000873 }
874
Seungjae Yoo62085c02022-08-12 04:44:52 +0000875 vm_config.name = config.name.clone();
Andrew Walbran3994f002022-01-27 17:33:45 +0000876 vm_config.protectedVm = config.protectedVm;
Jiyong Park032615f2022-01-10 13:55:34 +0900877 vm_config.numCpus = config.numCpus;
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900878 vm_config.taskProfiles = config.taskProfiles.clone();
Jiyong Park032615f2022-01-10 13:55:34 +0900879
Shikha Panwar22e70452022-10-10 18:32:55 +0000880 // Microdroid takes additional init ramdisk & (optionally) storage image
881 add_microdroid_system_images(config, instance_file, storage_image, &mut vm_config)?;
882
883 // Include Microdroid payload disk (contains apks, idsigs) in vm config
884 add_microdroid_payload_images(
Alan Stokes0d1ef782022-09-27 13:46:35 +0100885 config,
886 temporary_directory,
887 apk_file,
888 idsig_file,
Alan Stokes0d1ef782022-09-27 13:46:35 +0100889 &vm_payload_config,
890 &mut vm_config,
891 )?;
Jooyung Han21e9b922021-06-26 04:14:16 +0900892
Andrew Walbrancc0db522021-07-12 17:03:42 +0000893 Ok(vm_config)
Jooyung Han21e9b922021-06-26 04:14:16 +0900894}
895
Alan Stokes0d1ef782022-09-27 13:46:35 +0100896fn load_vm_payload_config_from_file(apk_file: &File, config_path: &str) -> Result<VmPayloadConfig> {
897 let mut apk_zip = ZipArchive::new(apk_file)?;
898 let config_file = apk_zip.by_name(config_path)?;
899 Ok(serde_json::from_reader(config_file)?)
900}
901
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000902fn create_vm_payload_config(
903 payload_config: &VirtualMachinePayloadConfig,
904) -> Result<VmPayloadConfig> {
Alan Stokes0d1ef782022-09-27 13:46:35 +0100905 // There isn't an actual config file. Construct a synthetic VmPayloadConfig from the explicit
906 // parameters we've been given. Microdroid will do something equivalent inside the VM using the
907 // payload config that we send it via the metadata file.
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000908
909 let payload_binary_name = &payload_config.payloadBinaryName;
910 if payload_binary_name.contains('/') {
911 bail!("Payload binary name must not specify a path: {payload_binary_name}");
912 }
913
914 let task = Task { type_: TaskType::MicrodroidLauncher, command: payload_binary_name.clone() };
915 Ok(VmPayloadConfig {
Alan Stokes0d1ef782022-09-27 13:46:35 +0100916 os: OsConfig { name: MICRODROID_OS_NAME.to_owned() },
917 task: Some(task),
918 apexes: vec![],
919 extra_apks: vec![],
920 prefer_staged: false,
Alan Stokes1f417c92022-09-29 15:13:28 +0100921 export_tombstones: false,
Alan Stokes0d1ef782022-09-27 13:46:35 +0100922 enable_authfs: false,
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000923 })
Alan Stokes0d1ef782022-09-27 13:46:35 +0100924}
925
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000926/// Generates a unique filename to use for a composite disk image.
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000927fn make_composite_image_filenames(
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000928 temporary_directory: &Path,
929 next_temporary_image_id: &mut u64,
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000930) -> CompositeImageFilenames {
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000931 let id = *next_temporary_image_id;
932 *next_temporary_image_id += 1;
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000933 CompositeImageFilenames {
934 composite: temporary_directory.join(format!("composite-{}.img", id)),
935 header: temporary_directory.join(format!("composite-{}-header.img", id)),
936 footer: temporary_directory.join(format!("composite-{}-footer.img", id)),
937 }
938}
939
940/// Filenames for a composite disk image, including header and footer partitions.
941#[derive(Clone, Debug, Eq, PartialEq)]
942struct CompositeImageFilenames {
943 /// The composite disk image itself.
944 composite: PathBuf,
945 /// The header partition image.
946 header: PathBuf,
947 /// The footer partition image.
948 footer: PathBuf,
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000949}
950
Jiyong Park753553b2021-07-12 21:21:09 +0900951/// Checks whether the caller has a specific permission
952fn check_permission(perm: &str) -> binder::Result<()> {
David Brazdil1f530702022-10-03 12:18:10 +0100953 let calling_pid = get_calling_pid();
954 let calling_uid = get_calling_uid();
Jiyong Park753553b2021-07-12 21:21:09 +0900955 // Root can do anything
956 if calling_uid == 0 {
957 return Ok(());
958 }
959 let perm_svc: Strong<dyn IPermissionController::IPermissionController> =
960 binder::get_interface("permission")?;
961 if perm_svc.checkPermission(perm, calling_pid, calling_uid as i32)? {
Andrew Walbran806f1542021-06-10 14:07:12 +0000962 Ok(())
963 } else {
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000964 Err(Status::new_exception_str(
Jiyong Park753553b2021-07-12 21:21:09 +0900965 ExceptionCode::SECURITY,
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000966 Some(format!("does not have the {} permission", perm)),
Jiyong Park753553b2021-07-12 21:21:09 +0900967 ))
Andrew Walbran806f1542021-06-10 14:07:12 +0000968 }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000969}
970
Jiyong Park753553b2021-07-12 21:21:09 +0900971/// Check whether the caller of the current Binder method is allowed to call debug methods.
972fn check_debug_access() -> binder::Result<()> {
973 check_permission("android.permission.DEBUG_VIRTUAL_MACHINE")
974}
975
976/// Check whether the caller of the current Binder method is allowed to manage VMs
977fn check_manage_access() -> binder::Result<()> {
978 check_permission("android.permission.MANAGE_VIRTUAL_MACHINE")
979}
980
Inseob Kim1119d702022-05-02 18:01:58 +0900981/// Check whether the caller of the current Binder method is allowed to create custom VMs
982fn check_use_custom_virtual_machine() -> binder::Result<()> {
983 check_permission("android.permission.USE_CUSTOM_VIRTUAL_MACHINE")
984}
985
Alan Stokes185fe112023-01-10 16:20:55 +0000986/// Return whether a partition is exempt from selinux label checks, because we know that it does
987/// not contain code and is likely to be generated in an app-writable directory.
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100988fn is_safe_app_partition(label: &str) -> bool {
Shikha Panwara2ff8c52022-11-30 19:25:46 +0000989 // See add_microdroid_system_images & add_microdroid_payload_images in payload.rs.
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100990 label == "vm-instance"
Shikha Panwara2ff8c52022-11-30 19:25:46 +0000991 || label == "encryptedstore"
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100992 || label == "microdroid-apk-idsig"
993 || label == "payload-metadata"
994 || label.starts_with("extra-idsig-")
995}
996
Alan Stokes185fe112023-01-10 16:20:55 +0000997/// Check that a file SELinux label is acceptable.
998///
999/// We only want to allow code in a VM to be sourced from places that apps, and the
1000/// system, do not have write access to.
1001///
1002/// Note that sepolicy must also grant read access for these types to both virtualization
1003/// service and crosvm.
1004///
1005/// App private data files are deliberately excluded, to avoid arbitrary payloads being run on
1006/// user devices (W^X).
1007fn check_label_is_allowed(context: &SeContext) -> Result<()> {
1008 match context.selinux_type()? {
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001009 | "system_file" // immutable dm-verity protected partition
1010 | "apk_data_file" // APKs of an installed app
Shikha Panwara2ff8c52022-11-30 19:25:46 +00001011 | "staging_data_file" // updated/staged APEX images
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001012 | "shell_data_file" // test files created via adb shell
1013 => Ok(()),
Alan Stokes185fe112023-01-10 16:20:55 +00001014 _ => bail!("Label {} is not allowed", context),
Jiyong Park029977d2021-11-24 21:56:49 +09001015 }
1016}
1017
Alan Stokes185fe112023-01-10 16:20:55 +00001018fn check_label_for_partition(partition: &Partition) -> Result<()> {
1019 let file = partition.image.as_ref().unwrap().as_ref();
1020 check_label_is_allowed(&getfilecon(file)?)
1021 .with_context(|| format!("Partition {} invalid", &partition.label))
1022}
1023
1024fn check_label_for_kernel_files(kernel: &Option<File>, initrd: &Option<File>) -> Result<()> {
1025 if let Some(f) = kernel {
1026 check_label_for_file(f, "kernel")?;
1027 }
1028 if let Some(f) = initrd {
1029 check_label_for_file(f, "initrd")?;
1030 }
1031 Ok(())
1032}
1033fn check_label_for_file(file: &File, name: &str) -> Result<()> {
1034 check_label_is_allowed(&getfilecon(file)?).with_context(|| format!("{} file invalid", name))
1035}
1036
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001037/// Implementation of the AIDL `IVirtualMachine` interface. Used as a handle to a VM.
1038#[derive(Debug)]
1039struct VirtualMachine {
1040 instance: Arc<VmInstance>,
1041}
1042
1043impl VirtualMachine {
1044 fn create(instance: Arc<VmInstance>) -> Strong<dyn IVirtualMachine> {
David Brazdil4b4c5102022-12-19 22:56:20 +00001045 BnVirtualMachine::new_binder(VirtualMachine { instance }, BinderFeatures::default())
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001046 }
1047}
1048
1049impl Interface for VirtualMachine {}
1050
1051impl IVirtualMachine for VirtualMachine {
1052 fn getCid(&self) -> binder::Result<i32> {
Jiyong Park753553b2021-07-12 21:21:09 +09001053 // Don't check permission. The owner of the VM might have passed this binder object to
1054 // others.
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001055 Ok(self.instance.cid as i32)
1056 }
Andrew Walbrandae07162021-03-12 17:05:20 +00001057
Andrew Walbran6b650662021-09-07 13:13:23 +00001058 fn getState(&self) -> binder::Result<VirtualMachineState> {
Jiyong Park753553b2021-07-12 21:21:09 +09001059 // Don't check permission. The owner of the VM might have passed this binder object to
1060 // others.
Andrew Walbran6b650662021-09-07 13:13:23 +00001061 Ok(get_state(&self.instance))
Andrew Walbrandae07162021-03-12 17:05:20 +00001062 }
1063
1064 fn registerCallback(
1065 &self,
1066 callback: &Strong<dyn IVirtualMachineCallback>,
1067 ) -> binder::Result<()> {
Jiyong Park753553b2021-07-12 21:21:09 +09001068 // Don't check permission. The owner of the VM might have passed this binder object to
1069 // others.
1070 //
Andrew Walbrandae07162021-03-12 17:05:20 +00001071 // TODO: Should this give an error if the VM is already dead?
1072 self.instance.callbacks.add(callback.clone());
1073 Ok(())
1074 }
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001075
Andrew Walbranf8d94112021-09-07 11:45:36 +00001076 fn start(&self) -> binder::Result<()> {
1077 self.instance.start().map_err(|e| {
1078 error!("Error starting VM with CID {}: {:?}", self.instance.cid, e);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001079 Status::new_service_specific_error_str(-1, Some(e.to_string()))
Andrew Walbranf8d94112021-09-07 11:45:36 +00001080 })
1081 }
1082
Inseob Kima446f802022-07-11 19:46:37 +09001083 fn stop(&self) -> binder::Result<()> {
1084 self.instance.kill().map_err(|e| {
1085 error!("Error stopping VM with CID {}: {:?}", self.instance.cid, e);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001086 Status::new_service_specific_error_str(-1, Some(e.to_string()))
Inseob Kima446f802022-07-11 19:46:37 +09001087 })
1088 }
1089
Keir Frasercdd4b112022-11-24 14:02:25 +00001090 fn onTrimMemory(&self, level: MemoryTrimLevel) -> binder::Result<()> {
1091 self.instance.trim_memory(level).map_err(|e| {
1092 error!("Error trimming VM with CID {}: {:?}", self.instance.cid, e);
1093 Status::new_service_specific_error_str(-1, Some(e.to_string()))
1094 })
1095 }
1096
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001097 fn connectVsock(&self, port: i32) -> binder::Result<ParcelFileDescriptor> {
Andrew Walbranf8d94112021-09-07 11:45:36 +00001098 if !matches!(&*self.instance.vm_state.lock().unwrap(), VmState::Running { .. }) {
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001099 return Err(Status::new_service_specific_error_str(-1, Some("VM is not running")));
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001100 }
Alan Stokes10c47672022-12-13 17:17:08 +00001101 let port = port as u32;
1102 if port < 1024 {
1103 return Err(Status::new_service_specific_error_str(
1104 -1,
1105 Some(format!("Can't connect to privileged port {port}")),
1106 ));
1107 }
1108 let stream = VsockStream::connect_with_cid_port(self.instance.cid, port).map_err(|e| {
1109 Status::new_service_specific_error_str(-1, Some(format!("Failed to connect: {:?}", e)))
1110 })?;
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001111 Ok(vsock_stream_to_pfd(stream))
1112 }
Andrew Walbrandae07162021-03-12 17:05:20 +00001113}
1114
1115impl Drop for VirtualMachine {
1116 fn drop(&mut self) {
1117 debug!("Dropping {:?}", self);
Inseob Kima446f802022-07-11 19:46:37 +09001118 if let Err(e) = self.instance.kill() {
1119 debug!("Error stopping dropped VM with CID {}: {:?}", self.instance.cid, e);
1120 }
Andrew Walbrandae07162021-03-12 17:05:20 +00001121 }
1122}
1123
1124/// A set of Binders to be called back in response to various events on the VM, such as when it
1125/// dies.
1126#[derive(Debug, Default)]
1127pub struct VirtualMachineCallbacks(Mutex<Vec<Strong<dyn IVirtualMachineCallback>>>);
1128
1129impl VirtualMachineCallbacks {
Jiyong Park8611a6c2021-07-09 18:17:44 +09001130 /// Call all registered callbacks to notify that the payload has started.
David Brazdil451cc962022-10-14 14:08:12 +01001131 pub fn notify_payload_started(&self, cid: Cid) {
Jiyong Park8611a6c2021-07-09 18:17:44 +09001132 let callbacks = &*self.0.lock().unwrap();
Jiyong Park8611a6c2021-07-09 18:17:44 +09001133 for callback in callbacks {
David Brazdil451cc962022-10-14 14:08:12 +01001134 if let Err(e) = callback.onPayloadStarted(cid as i32) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001135 error!("Error notifying payload start event from VM CID {}: {:?}", cid, e);
Jiyong Park8611a6c2021-07-09 18:17:44 +09001136 }
1137 }
1138 }
1139
Inseob Kim14cb8692021-08-31 21:50:39 +09001140 /// Call all registered callbacks to notify that the payload is ready to serve.
1141 pub fn notify_payload_ready(&self, cid: Cid) {
1142 let callbacks = &*self.0.lock().unwrap();
1143 for callback in callbacks {
1144 if let Err(e) = callback.onPayloadReady(cid as i32) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001145 error!("Error notifying payload ready event from VM CID {}: {:?}", cid, e);
Inseob Kim14cb8692021-08-31 21:50:39 +09001146 }
1147 }
1148 }
1149
Inseob Kim2444af92021-08-31 01:22:50 +09001150 /// Call all registered callbacks to notify that the payload has finished.
1151 pub fn notify_payload_finished(&self, cid: Cid, exit_code: i32) {
1152 let callbacks = &*self.0.lock().unwrap();
1153 for callback in callbacks {
1154 if let Err(e) = callback.onPayloadFinished(cid as i32, exit_code) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001155 error!("Error notifying payload finish event from VM CID {}: {:?}", cid, e);
Inseob Kim2444af92021-08-31 01:22:50 +09001156 }
1157 }
1158 }
1159
Jooyung Handd0a1732021-11-23 15:26:20 +09001160 /// Call all registered callbacks to say that the VM encountered an error.
Alan Stokes2bead0d2022-09-05 16:58:34 +01001161 pub fn notify_error(&self, cid: Cid, error_code: ErrorCode, message: &str) {
Jooyung Handd0a1732021-11-23 15:26:20 +09001162 let callbacks = &*self.0.lock().unwrap();
1163 for callback in callbacks {
1164 if let Err(e) = callback.onError(cid as i32, error_code, message) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001165 error!("Error notifying error event from VM CID {}: {:?}", cid, e);
Jooyung Handd0a1732021-11-23 15:26:20 +09001166 }
1167 }
1168 }
1169
Andrew Walbrandae07162021-03-12 17:05:20 +00001170 /// Call all registered callbacks to say that the VM has died.
Andrew Walbranc92d35f2022-01-12 12:45:19 +00001171 pub fn callback_on_died(&self, cid: Cid, reason: DeathReason) {
Andrew Walbrandae07162021-03-12 17:05:20 +00001172 let callbacks = &*self.0.lock().unwrap();
1173 for callback in callbacks {
Andrew Walbranc92d35f2022-01-12 12:45:19 +00001174 if let Err(e) = callback.onDied(cid as i32, reason) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001175 error!("Error notifying exit of VM CID {}: {:?}", cid, e);
Andrew Walbrandae07162021-03-12 17:05:20 +00001176 }
1177 }
1178 }
1179
1180 /// Add a new callback to the set.
1181 fn add(&self, callback: Strong<dyn IVirtualMachineCallback>) {
1182 self.0.lock().unwrap().push(callback);
1183 }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001184}
1185
Andrew Walbranf6bf6862021-05-21 12:41:13 +00001186/// The mutable state of the VirtualizationService. There should only be one instance of this
1187/// struct.
Chris Wailes641fc4a2021-12-01 15:03:21 -08001188#[derive(Debug, Default)]
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001189struct State {
Andrew Walbran320b5602021-03-04 16:11:12 +00001190 /// The VMs which have been started. When VMs are started a weak reference is added to this list
1191 /// while a strong reference is returned to the caller over Binder. Once all copies of the
1192 /// Binder client are dropped the weak reference here will become invalid, and will be removed
1193 /// from the list opportunistically the next time `add_vm` is called.
1194 vms: Vec<Weak<VmInstance>>,
1195}
1196
1197impl State {
Andrew Walbrandae07162021-03-12 17:05:20 +00001198 /// Get a list of VMs which still have Binder references to them.
Andrew Walbran320b5602021-03-04 16:11:12 +00001199 fn vms(&self) -> Vec<Arc<VmInstance>> {
1200 // Attempt to upgrade the weak pointers to strong pointers.
1201 self.vms.iter().filter_map(Weak::upgrade).collect()
1202 }
1203
1204 /// Add a new VM to the list.
1205 fn add_vm(&mut self, vm: Weak<VmInstance>) {
1206 // Garbage collect any entries from the stored list which no longer exist.
1207 self.vms.retain(|vm| vm.strong_count() > 0);
1208
1209 // Actually add the new VM.
1210 self.vms.push(vm);
1211 }
David Brazdil3c2ddef2021-03-18 13:09:57 +00001212
Jiyong Park8611a6c2021-07-09 18:17:44 +09001213 /// Get a VM that corresponds to the given cid
1214 fn get_vm(&self, cid: Cid) -> Option<Arc<VmInstance>> {
1215 self.vms().into_iter().find(|vm| vm.cid == cid)
1216 }
Jiyong Parkd50a0242021-09-16 21:00:14 +09001217}
1218
Andrew Walbran6b650662021-09-07 13:13:23 +00001219/// Gets the `VirtualMachineState` of the given `VmInstance`.
1220fn get_state(instance: &VmInstance) -> VirtualMachineState {
Andrew Walbranf8d94112021-09-07 11:45:36 +00001221 match &*instance.vm_state.lock().unwrap() {
1222 VmState::NotStarted { .. } => VirtualMachineState::NOT_STARTED,
1223 VmState::Running { .. } => match instance.payload_state() {
Andrew Walbran6b650662021-09-07 13:13:23 +00001224 PayloadState::Starting => VirtualMachineState::STARTING,
1225 PayloadState::Started => VirtualMachineState::STARTED,
1226 PayloadState::Ready => VirtualMachineState::READY,
1227 PayloadState::Finished => VirtualMachineState::FINISHED,
Jiyong Parka4eebde2022-07-12 18:01:12 +09001228 PayloadState::Hangup => VirtualMachineState::DEAD,
Andrew Walbranf8d94112021-09-07 11:45:36 +00001229 },
1230 VmState::Dead => VirtualMachineState::DEAD,
1231 VmState::Failed => VirtualMachineState::DEAD,
Andrew Walbran6b650662021-09-07 13:13:23 +00001232 }
1233}
1234
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +00001235/// Converts a `&ParcelFileDescriptor` to a `File` by cloning the file.
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +00001236pub fn clone_file(file: &ParcelFileDescriptor) -> Result<File, Status> {
Andrew Walbran806f1542021-06-10 14:07:12 +00001237 file.as_ref().try_clone().map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001238 Status::new_exception_str(
Andrew Walbran806f1542021-06-10 14:07:12 +00001239 ExceptionCode::BAD_PARCELABLE,
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001240 Some(format!("Failed to clone File from ParcelFileDescriptor: {:?}", e)),
Andrew Walbran806f1542021-06-10 14:07:12 +00001241 )
1242 })
1243}
1244
Andrew Walbrand3a84182021-09-07 14:48:52 +00001245/// Converts an `&Option<ParcelFileDescriptor>` to an `Option<File>` by cloning the file.
1246fn maybe_clone_file(file: &Option<ParcelFileDescriptor>) -> Result<Option<File>, Status> {
1247 file.as_ref().map(clone_file).transpose()
1248}
1249
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001250/// Converts a `VsockStream` to a `ParcelFileDescriptor`.
1251fn vsock_stream_to_pfd(stream: VsockStream) -> ParcelFileDescriptor {
1252 // SAFETY: ownership is transferred from stream to f
1253 let f = unsafe { File::from_raw_fd(stream.into_raw_fd()) };
1254 ParcelFileDescriptor::new(f)
1255}
1256
Jiyong Parkdcf17412022-02-08 15:07:23 +09001257/// Parses the platform version requirement string.
1258fn parse_platform_version_req(s: &str) -> Result<VersionReq, Status> {
1259 VersionReq::parse(s).map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001260 Status::new_exception_str(
Jiyong Parkdcf17412022-02-08 15:07:23 +09001261 ExceptionCode::BAD_PARCELABLE,
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001262 Some(format!("Invalid platform version requirement {}: {:?}", s, e)),
Jiyong Parkdcf17412022-02-08 15:07:23 +09001263 )
1264 })
1265}
1266
Inseob Kim0168b462022-12-27 14:54:35 +09001267fn is_debuggable(config: &VirtualMachineConfig) -> bool {
1268 match config {
1269 VirtualMachineConfig::AppConfig(config) => config.debugLevel != DebugLevel::NONE,
1270 _ => false,
1271 }
1272}
1273
1274fn clone_or_prepare_logger_fd(
1275 config: &VirtualMachineConfig,
1276 fd: Option<&ParcelFileDescriptor>,
1277 tag: String,
1278) -> Result<Option<File>, Status> {
1279 if let Some(fd) = fd {
1280 return Ok(Some(clone_file(fd)?));
1281 }
1282
1283 if !is_debuggable(config) {
1284 return Ok(None);
1285 }
1286
1287 let (raw_read_fd, raw_write_fd) = pipe().map_err(|e| {
1288 Status::new_service_specific_error_str(-1, Some(format!("Failed to create pipe: {:?}", e)))
1289 })?;
1290
1291 // SAFETY: We are the sole owners of these fds as they were just created.
1292 let mut reader = BufReader::new(unsafe { File::from_raw_fd(raw_read_fd) });
1293 let write_fd = unsafe { File::from_raw_fd(raw_write_fd) };
1294
1295 std::thread::spawn(move || loop {
1296 let mut buf = vec![];
1297 match reader.read_until(b'\n', &mut buf) {
1298 Ok(0) => {
1299 // EOF
1300 return;
1301 }
1302 Ok(size) => {
1303 if buf[size - 1] == b'\n' {
1304 buf.pop();
1305 }
1306 info!("{}: {}", &tag, &String::from_utf8_lossy(&buf));
1307 }
1308 Err(e) => {
1309 error!("Could not read console pipe: {:?}", e);
1310 return;
1311 }
1312 };
1313 });
1314
1315 Ok(Some(write_fd))
1316}
1317
Jooyung Han35edb8f2021-07-01 16:17:16 +09001318/// Simple utility for referencing Borrowed or Owned. Similar to std::borrow::Cow, but
1319/// it doesn't require that T implements Clone.
1320enum BorrowedOrOwned<'a, T> {
1321 Borrowed(&'a T),
1322 Owned(T),
1323}
1324
1325impl<'a, T> AsRef<T> for BorrowedOrOwned<'a, T> {
1326 fn as_ref(&self) -> &T {
1327 match self {
1328 Self::Borrowed(b) => b,
Chris Wailes68c39f82021-07-27 16:03:44 -07001329 Self::Owned(o) => o,
Jooyung Han35edb8f2021-07-01 16:17:16 +09001330 }
1331 }
1332}
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001333
1334/// Implementation of `IVirtualMachineService`, the entry point of the AIDL service.
1335#[derive(Debug, Default)]
1336struct VirtualMachineService {
1337 state: Arc<Mutex<State>>,
Inseob Kimc7d28c72021-10-25 14:28:10 +00001338 cid: Cid,
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001339}
1340
1341impl Interface for VirtualMachineService {}
1342
1343impl IVirtualMachineService for VirtualMachineService {
Inseob Kimc7d28c72021-10-25 14:28:10 +00001344 fn notifyPayloadStarted(&self) -> binder::Result<()> {
1345 let cid = self.cid;
Inseob Kim7f61fe72021-08-20 20:50:47 +09001346 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001347 info!("VM with CID {} started payload", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001348 vm.update_payload_state(PayloadState::Started).map_err(|e| {
1349 Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
1350 })?;
David Brazdil451cc962022-10-14 14:08:12 +01001351 vm.callbacks.notify_payload_started(cid);
Seungjae Yoo62085c02022-08-12 04:44:52 +00001352
Seungjae Yoo6d265d92022-11-15 10:51:33 +09001353 let vm_start_timestamp = vm.vm_metric.lock().unwrap().start_timestamp;
1354 write_vm_booted_stats(vm.requester_uid as i32, &vm.name, vm_start_timestamp);
Inseob Kim7f61fe72021-08-20 20:50:47 +09001355 Ok(())
1356 } else {
Jooyung Handd0a1732021-11-23 15:26:20 +09001357 error!("notifyPayloadStarted is called from an unknown CID {}", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001358 Err(Status::new_service_specific_error_str(
1359 -1,
1360 Some(format!("cannot find a VM with CID {}", cid)),
Inseob Kim7f61fe72021-08-20 20:50:47 +09001361 ))
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001362 }
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001363 }
Inseob Kim2444af92021-08-31 01:22:50 +09001364
Inseob Kimc7d28c72021-10-25 14:28:10 +00001365 fn notifyPayloadReady(&self) -> binder::Result<()> {
1366 let cid = self.cid;
Inseob Kim14cb8692021-08-31 21:50:39 +09001367 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001368 info!("VM with CID {} reported payload is ready", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001369 vm.update_payload_state(PayloadState::Ready).map_err(|e| {
1370 Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
1371 })?;
Inseob Kim14cb8692021-08-31 21:50:39 +09001372 vm.callbacks.notify_payload_ready(cid);
1373 Ok(())
1374 } else {
Jooyung Handd0a1732021-11-23 15:26:20 +09001375 error!("notifyPayloadReady is called from an unknown CID {}", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001376 Err(Status::new_service_specific_error_str(
1377 -1,
1378 Some(format!("cannot find a VM with CID {}", cid)),
Inseob Kim14cb8692021-08-31 21:50:39 +09001379 ))
1380 }
1381 }
1382
Inseob Kimc7d28c72021-10-25 14:28:10 +00001383 fn notifyPayloadFinished(&self, exit_code: i32) -> binder::Result<()> {
1384 let cid = self.cid;
Inseob Kim2444af92021-08-31 01:22:50 +09001385 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001386 info!("VM with CID {} finished payload", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001387 vm.update_payload_state(PayloadState::Finished).map_err(|e| {
1388 Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
1389 })?;
Inseob Kim2444af92021-08-31 01:22:50 +09001390 vm.callbacks.notify_payload_finished(cid, exit_code);
1391 Ok(())
1392 } else {
Jooyung Handd0a1732021-11-23 15:26:20 +09001393 error!("notifyPayloadFinished is called from an unknown CID {}", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001394 Err(Status::new_service_specific_error_str(
1395 -1,
1396 Some(format!("cannot find a VM with CID {}", cid)),
Jooyung Handd0a1732021-11-23 15:26:20 +09001397 ))
1398 }
1399 }
1400
Alan Stokes2bead0d2022-09-05 16:58:34 +01001401 fn notifyError(&self, error_code: ErrorCode, message: &str) -> binder::Result<()> {
Jooyung Handd0a1732021-11-23 15:26:20 +09001402 let cid = self.cid;
1403 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001404 info!("VM with CID {} encountered an error", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001405 vm.update_payload_state(PayloadState::Finished).map_err(|e| {
1406 Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
1407 })?;
Jooyung Handd0a1732021-11-23 15:26:20 +09001408 vm.callbacks.notify_error(cid, error_code, message);
1409 Ok(())
1410 } else {
Seungjae Yooec8c1602022-06-20 05:28:00 +00001411 error!("notifyError is called from an unknown CID {}", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001412 Err(Status::new_service_specific_error_str(
1413 -1,
1414 Some(format!("cannot find a VM with CID {}", cid)),
Inseob Kim2444af92021-08-31 01:22:50 +09001415 ))
1416 }
1417 }
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001418}
1419
1420impl VirtualMachineService {
Inseob Kimc7d28c72021-10-25 14:28:10 +00001421 fn new_binder(state: Arc<Mutex<State>>, cid: Cid) -> Strong<dyn IVirtualMachineService> {
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001422 BnVirtualMachineService::new_binder(
Inseob Kimc7d28c72021-10-25 14:28:10 +00001423 VirtualMachineService { state, cid },
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001424 BinderFeatures::default(),
1425 )
1426 }
1427}
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001428
1429#[cfg(test)]
1430mod tests {
1431 use super::*;
1432
1433 #[test]
1434 fn test_is_allowed_label_for_partition() -> Result<()> {
1435 let expected_results = vec![
1436 ("u:object_r:system_file:s0", true),
1437 ("u:object_r:apk_data_file:s0", true),
1438 ("u:object_r:app_data_file:s0", false),
1439 ("u:object_r:app_data_file:s0:c512,c768", false),
1440 ("u:object_r:privapp_data_file:s0:c512,c768", false),
1441 ("invalid", false),
1442 ("user:role:apk_data_file:severity:categories", true),
1443 ("user:role:apk_data_file:severity:categories:extraneous", false),
1444 ];
1445
1446 for (label, expected_valid) in expected_results {
1447 let context = SeContext::new(label)?;
1448 let result = check_label_is_allowed(&context);
1449 if expected_valid {
1450 assert!(result.is_ok(), "Expected label {} to be allowed, got {:?}", label, result);
1451 } else if result.is_ok() {
1452 bail!("Expected label {} to be disallowed", label);
1453 }
1454 }
1455 Ok(())
1456 }
Nikita Ioffef1ce9872022-12-09 13:31:59 +00001457
1458 #[test]
1459 fn test_create_or_update_idsig_file_empty_apk() -> Result<()> {
1460 let apk = tempfile::tempfile().unwrap();
1461 let idsig = tempfile::tempfile().unwrap();
1462
1463 let ret = create_or_update_idsig_file(
1464 &ParcelFileDescriptor::new(apk),
1465 &ParcelFileDescriptor::new(idsig),
1466 );
1467 assert!(ret.is_err(), "should fail");
1468 Ok(())
1469 }
1470
1471 #[test]
1472 fn test_create_or_update_idsig_dir_instead_of_file_for_apk() -> Result<()> {
1473 let tmp_dir = tempfile::TempDir::new().unwrap();
1474 let apk = File::open(tmp_dir.path()).unwrap();
1475 let idsig = tempfile::tempfile().unwrap();
1476
1477 let ret = create_or_update_idsig_file(
1478 &ParcelFileDescriptor::new(apk),
1479 &ParcelFileDescriptor::new(idsig),
1480 );
1481 assert!(ret.is_err(), "should fail");
1482 Ok(())
1483 }
1484
1485 /// Verifies that create_or_update_idsig_file won't oom if a fd that corresponds to a directory
1486 /// on ext4 filesystem is passed.
1487 /// On ext4 lseek on a directory fd will return (off_t)-1 (see:
1488 /// https://bugzilla.kernel.org/show_bug.cgi?id=200043), which will result in
1489 /// create_or_update_idsig_file ooming while attempting to allocate petabytes of memory.
1490 #[test]
1491 fn test_create_or_update_idsig_does_not_crash_dir_on_ext4() -> Result<()> {
1492 // APEXes are backed by the ext4.
1493 let apk = File::open("/apex/com.android.virt/").unwrap();
1494 let idsig = tempfile::tempfile().unwrap();
1495
1496 let ret = create_or_update_idsig_file(
1497 &ParcelFileDescriptor::new(apk),
1498 &ParcelFileDescriptor::new(idsig),
1499 );
1500 assert!(ret.is_err(), "should fail");
1501 Ok(())
1502 }
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001503}