blob: bf49fefa06160ea304138fc81f4bdb36a7b57ee3 [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 Brazdil209074a2023-01-12 16:44:51 +0000197 check_manage_access()?;
198
David Brazdil31c22e62023-01-11 13:52:27 +0000199 let requester_uid = get_calling_uid();
David Brazdild4f51a52023-01-11 14:09:27 +0000200 let requester_debug_pid = requester_debug_pid as pid_t;
David Brazdil528e0472022-10-10 15:06:02 +0100201 let state = &mut *self.state.lock().unwrap();
David Brazdild4f51a52023-01-11 14:09:27 +0000202 state.allocate_vm_context(requester_uid, requester_debug_pid).map_err(|e| {
David Brazdil528e0472022-10-10 15:06:02 +0100203 Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
David Brazdil4b4c5102022-12-19 22:56:20 +0000204 })
David Brazdil528e0472022-10-10 15:06:02 +0100205 }
David Brazdil49f96f52022-12-16 21:29:13 +0000206
207 fn atomVmBooted(&self, atom: &AtomVmBooted) -> Result<(), Status> {
208 forward_vm_booted_atom(atom);
209 Ok(())
210 }
211
212 fn atomVmCreationRequested(&self, atom: &AtomVmCreationRequested) -> Result<(), Status> {
213 forward_vm_creation_atom(atom);
214 Ok(())
215 }
216
217 fn atomVmExited(&self, atom: &AtomVmExited) -> Result<(), Status> {
218 forward_vm_exited_atom(atom);
219 Ok(())
220 }
David Brazdild4f51a52023-01-11 14:09:27 +0000221
222 fn debugListVms(&self) -> binder::Result<Vec<VirtualMachineDebugInfo>> {
David Brazdil209074a2023-01-12 16:44:51 +0000223 check_debug_access()?;
224
David Brazdild4f51a52023-01-11 14:09:27 +0000225 let state = &mut *self.state.lock().unwrap();
226 let cids = state
227 .held_contexts
228 .iter()
229 .filter_map(|(_, inst)| Weak::upgrade(inst))
230 .map(|vm| VirtualMachineDebugInfo {
231 cid: vm.cid as i32,
232 temporaryDirectory: vm.get_temp_dir().to_string_lossy().to_string(),
233 requesterUid: vm.requester_uid as i32,
234 requesterPid: vm.requester_debug_pid as i32,
235 })
236 .collect();
237 Ok(cids)
238 }
David Brazdil528e0472022-10-10 15:06:02 +0100239}
240
David Brazdil31c22e62023-01-11 13:52:27 +0000241#[derive(Debug, Default)]
242struct GlobalVmInstance {
243 /// The unique CID assigned to the VM for vsock communication.
244 cid: Cid,
David Brazdild4f51a52023-01-11 14:09:27 +0000245 /// UID of the client who requested this VM instance.
246 requester_uid: uid_t,
247 /// PID of the client who requested this VM instance.
248 requester_debug_pid: pid_t,
David Brazdil31c22e62023-01-11 13:52:27 +0000249}
250
251impl GlobalVmInstance {
252 fn get_temp_dir(&self) -> PathBuf {
253 let cid = self.cid;
254 format!("{TEMPORARY_DIRECTORY}/{cid}").into()
255 }
David Brazdil528e0472022-10-10 15:06:02 +0100256}
257
258/// The mutable state of the VirtualizationServiceInternal. There should only be one instance
259/// of this struct.
260#[derive(Debug, Default)]
David Brazdil73988ea2022-11-11 15:10:32 +0000261struct GlobalState {
David Brazdil31c22e62023-01-11 13:52:27 +0000262 /// VM contexts currently allocated to running VMs. A CID is never recycled as long
David Brazdil73988ea2022-11-11 15:10:32 +0000263 /// as there is a strong reference held by a GlobalVmContext.
David Brazdil31c22e62023-01-11 13:52:27 +0000264 held_contexts: HashMap<Cid, Weak<GlobalVmInstance>>,
David Brazdil73988ea2022-11-11 15:10:32 +0000265}
David Brazdil528e0472022-10-10 15:06:02 +0100266
267impl GlobalState {
268 /// Get the next available CID, or an error if we have run out. The last CID used is stored in
269 /// a system property so that restart of virtualizationservice doesn't reuse CID while the host
270 /// Android is up.
David Brazdil31c22e62023-01-11 13:52:27 +0000271 fn get_next_available_cid(&mut self) -> Result<Cid> {
David Brazdil73988ea2022-11-11 15:10:32 +0000272 // Start trying to find a CID from the last used CID + 1. This ensures
David Brazdil8cf8f482022-11-23 14:21:26 +0000273 // that we do not eagerly recycle CIDs. It makes debugging easier but
274 // also means that retrying to allocate a CID, eg. because it is
275 // erroneously occupied by a process, will not recycle the same CID.
David Brazdil73988ea2022-11-11 15:10:32 +0000276 let last_cid_prop =
277 system_properties::read(SYSPROP_LAST_CID)?.and_then(|val| match val.parse::<Cid>() {
278 Ok(num) => {
279 if is_valid_guest_cid(num) {
280 Some(num)
281 } else {
282 error!("Invalid value '{}' of property '{}'", num, SYSPROP_LAST_CID);
283 None
284 }
285 }
David Brazdil528e0472022-10-10 15:06:02 +0100286 Err(_) => {
287 error!("Invalid value '{}' of property '{}'", val, SYSPROP_LAST_CID);
David Brazdil73988ea2022-11-11 15:10:32 +0000288 None
David Brazdil528e0472022-10-10 15:06:02 +0100289 }
David Brazdil73988ea2022-11-11 15:10:32 +0000290 });
291
292 let first_cid = if let Some(last_cid) = last_cid_prop {
David Brazdil31c22e62023-01-11 13:52:27 +0000293 if last_cid == GUEST_CID_MAX {
294 GUEST_CID_MIN
295 } else {
296 last_cid + 1
297 }
David Brazdil73988ea2022-11-11 15:10:32 +0000298 } else {
299 GUEST_CID_MIN
David Brazdil528e0472022-10-10 15:06:02 +0100300 };
David Brazdil73988ea2022-11-11 15:10:32 +0000301
302 let cid = self
303 .find_available_cid(first_cid..=GUEST_CID_MAX)
David Brazdil31c22e62023-01-11 13:52:27 +0000304 .or_else(|| self.find_available_cid(GUEST_CID_MIN..first_cid))
305 .ok_or_else(|| anyhow!("Could not find an available CID."))?;
David Brazdil73988ea2022-11-11 15:10:32 +0000306
David Brazdil31c22e62023-01-11 13:52:27 +0000307 system_properties::write(SYSPROP_LAST_CID, &format!("{}", cid))?;
308 Ok(cid)
David Brazdil73988ea2022-11-11 15:10:32 +0000309 }
310
311 fn find_available_cid<I>(&self, mut range: I) -> Option<Cid>
312 where
313 I: Iterator<Item = Cid>,
314 {
David Brazdil31c22e62023-01-11 13:52:27 +0000315 range.find(|cid| !self.held_contexts.contains_key(cid))
David Brazdil528e0472022-10-10 15:06:02 +0100316 }
David Brazdil4b4c5102022-12-19 22:56:20 +0000317
David Brazdil31c22e62023-01-11 13:52:27 +0000318 fn allocate_vm_context(
319 &mut self,
320 requester_uid: uid_t,
David Brazdild4f51a52023-01-11 14:09:27 +0000321 requester_debug_pid: pid_t,
David Brazdil31c22e62023-01-11 13:52:27 +0000322 ) -> Result<Strong<dyn IGlobalVmContext>> {
323 // Garbage collect unused VM contexts.
324 self.held_contexts.retain(|_, instance| instance.strong_count() > 0);
325
326 let cid = self.get_next_available_cid()?;
David Brazdild4f51a52023-01-11 14:09:27 +0000327 let instance = Arc::new(GlobalVmInstance { cid, requester_uid, requester_debug_pid });
David Brazdil31c22e62023-01-11 13:52:27 +0000328 create_temporary_directory(&instance.get_temp_dir(), requester_uid)?;
329
330 self.held_contexts.insert(cid, Arc::downgrade(&instance));
331 let binder = GlobalVmContext { instance, ..Default::default() };
David Brazdil4b4c5102022-12-19 22:56:20 +0000332 Ok(BnGlobalVmContext::new_binder(binder, BinderFeatures::default()))
333 }
334}
335
David Brazdil31c22e62023-01-11 13:52:27 +0000336fn create_temporary_directory(path: &PathBuf, requester_uid: uid_t) -> Result<()> {
David Brazdil4b4c5102022-12-19 22:56:20 +0000337 if path.as_path().exists() {
David Brazdil31c22e62023-01-11 13:52:27 +0000338 remove_temporary_dir(path).unwrap_or_else(|e| {
David Brazdil4b4c5102022-12-19 22:56:20 +0000339 warn!("Could not delete temporary directory {:?}: {}", path, e);
340 });
341 }
342 // Create a directory that is owned by client's UID but system's GID, and permissions 0700.
343 // If the chown() fails, this will leave behind an empty directory that will get removed
344 // at the next attempt, or if virtualizationservice is restarted.
David Brazdil31c22e62023-01-11 13:52:27 +0000345 create_dir(path).with_context(|| format!("Could not create temporary directory {:?}", path))?;
346 chown(path, Some(Uid::from_raw(requester_uid)), None)
David Brazdil4b4c5102022-12-19 22:56:20 +0000347 .with_context(|| format!("Could not set ownership of temporary directory {:?}", path))?;
David Brazdil31c22e62023-01-11 13:52:27 +0000348 Ok(())
David Brazdil4b4c5102022-12-19 22:56:20 +0000349}
350
351/// Removes a directory owned by a different user by first changing its owner back
352/// to VirtualizationService.
353pub fn remove_temporary_dir(path: &PathBuf) -> Result<()> {
354 if !path.as_path().is_dir() {
355 bail!("Path {:?} is not a directory", path);
356 }
357 chown(path, Some(Uid::current()), None)?;
358 set_permissions(path, Permissions::from_mode(0o700))?;
359 remove_temporary_files(path)?;
360 remove_dir(path)?;
361 Ok(())
362}
363
364pub fn remove_temporary_files(path: &PathBuf) -> Result<()> {
365 for dir_entry in read_dir(path)? {
366 remove_file(dir_entry?.path())?;
367 }
368 Ok(())
David Brazdil528e0472022-10-10 15:06:02 +0100369}
370
371/// Implementation of the AIDL `IGlobalVmContext` interface.
372#[derive(Debug, Default)]
373struct GlobalVmContext {
David Brazdil31c22e62023-01-11 13:52:27 +0000374 /// Strong reference to the context's instance data structure.
375 instance: Arc<GlobalVmInstance>,
David Brazdil73988ea2022-11-11 15:10:32 +0000376 /// Keeps our service process running as long as this VM context exists.
David Brazdil528e0472022-10-10 15:06:02 +0100377 #[allow(dead_code)]
378 lazy_service_guard: LazyServiceGuard,
379}
380
David Brazdil528e0472022-10-10 15:06:02 +0100381impl Interface for GlobalVmContext {}
382
383impl IGlobalVmContext for GlobalVmContext {
384 fn getCid(&self) -> binder::Result<i32> {
David Brazdil31c22e62023-01-11 13:52:27 +0000385 Ok(self.instance.cid as i32)
David Brazdil528e0472022-10-10 15:06:02 +0100386 }
David Brazdil4b4c5102022-12-19 22:56:20 +0000387
388 fn getTemporaryDirectory(&self) -> binder::Result<String> {
David Brazdil31c22e62023-01-11 13:52:27 +0000389 Ok(self.instance.get_temp_dir().to_string_lossy().to_string())
David Brazdil4b4c5102022-12-19 22:56:20 +0000390 }
David Brazdil528e0472022-10-10 15:06:02 +0100391}
392
393/// Implementation of `IVirtualizationService`, the entry point of the AIDL service.
David Brazdil49f96f52022-12-16 21:29:13 +0000394#[derive(Debug, Default)]
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000395pub struct VirtualizationService {
Jiyong Park8611a6c2021-07-09 18:17:44 +0900396 state: Arc<Mutex<State>>,
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000397}
398
Shikha Panward8e35422021-10-11 13:51:27 +0000399impl Interface for VirtualizationService {
400 fn dump(&self, mut file: &File, _args: &[&CStr]) -> Result<(), StatusCode> {
401 check_permission("android.permission.DUMP").or(Err(StatusCode::PERMISSION_DENIED))?;
402 let state = &mut *self.state.lock().unwrap();
403 let vms = state.vms();
404 writeln!(file, "Running {0} VMs:", vms.len()).or(Err(StatusCode::UNKNOWN_ERROR))?;
405 for vm in vms {
406 writeln!(file, "VM CID: {}", vm.cid).or(Err(StatusCode::UNKNOWN_ERROR))?;
407 writeln!(file, "\tState: {:?}", vm.vm_state.lock().unwrap())
408 .or(Err(StatusCode::UNKNOWN_ERROR))?;
409 writeln!(file, "\tPayload state {:?}", vm.payload_state())
410 .or(Err(StatusCode::UNKNOWN_ERROR))?;
411 writeln!(file, "\tProtected: {}", vm.protected).or(Err(StatusCode::UNKNOWN_ERROR))?;
412 writeln!(file, "\ttemporary_directory: {}", vm.temporary_directory.to_string_lossy())
413 .or(Err(StatusCode::UNKNOWN_ERROR))?;
414 writeln!(file, "\trequester_uid: {}", vm.requester_uid)
415 .or(Err(StatusCode::UNKNOWN_ERROR))?;
Shikha Panward8e35422021-10-11 13:51:27 +0000416 writeln!(file, "\trequester_debug_pid: {}", vm.requester_debug_pid)
417 .or(Err(StatusCode::UNKNOWN_ERROR))?;
418 }
419 Ok(())
420 }
421}
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000422
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000423impl IVirtualizationService for VirtualizationService {
Andrew Walbranf8d94112021-09-07 11:45:36 +0000424 /// Creates (but does not start) a new VM with the given configuration, assigning it the next
425 /// available CID.
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000426 ///
427 /// Returns a binder `IVirtualMachine` object referring to it, as a handle for the client.
Andrew Walbranf8d94112021-09-07 11:45:36 +0000428 fn createVm(
Andrew Walbrana89fc132021-03-17 17:08:36 +0000429 &self,
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000430 config: &VirtualMachineConfig,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900431 console_fd: Option<&ParcelFileDescriptor>,
Andrew Walbrana89fc132021-03-17 17:08:36 +0000432 log_fd: Option<&ParcelFileDescriptor>,
433 ) -> binder::Result<Strong<dyn IVirtualMachine>> {
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000434 let mut is_protected = false;
435 let ret = self.create_vm_internal(config, console_fd, log_fd, &mut is_protected);
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +0000436 write_vm_creation_stats(config, is_protected, &ret);
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000437 ret
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000438 }
Andrew Walbran320b5602021-03-04 16:11:12 +0000439
Andrew Walbrandff3b942021-06-09 15:20:36 +0000440 /// Initialise an empty partition image of the given size to be used as a writable partition.
441 fn initializeWritablePartition(
442 &self,
443 image_fd: &ParcelFileDescriptor,
444 size: i64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900445 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000446 ) -> binder::Result<()> {
Jiyong Park753553b2021-07-12 21:21:09 +0900447 check_manage_access()?;
Andrew Walbrandfc953d2021-06-10 13:59:56 +0000448 let size = size.try_into().map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000449 Status::new_exception_str(
Andrew Walbran806f1542021-06-10 14:07:12 +0000450 ExceptionCode::ILLEGAL_ARGUMENT,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100451 Some(format!("Invalid size {}: {:?}", size, e)),
Andrew Walbran806f1542021-06-10 14:07:12 +0000452 )
Andrew Walbrandff3b942021-06-09 15:20:36 +0000453 })?;
Andrew Walbrandfc953d2021-06-10 13:59:56 +0000454 let image = clone_file(image_fd)?;
Jooyung Han1edd5b92021-10-28 10:58:05 +0900455 // initialize the file. Any data in the file will be erased.
456 image.set_len(0).map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000457 Status::new_service_specific_error_str(
458 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100459 Some(format!("Failed to reset a file: {:?}", e)),
Jooyung Han1edd5b92021-10-28 10:58:05 +0900460 )
461 })?;
Jiyong Park9dd389e2021-08-23 20:42:59 +0900462 let mut part = QcowFile::new(image, size).map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000463 Status::new_service_specific_error_str(
464 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100465 Some(format!("Failed to create QCOW2 image: {:?}", e)),
Andrew Walbran806f1542021-06-10 14:07:12 +0000466 )
Andrew Walbrandfc953d2021-06-10 13:59:56 +0000467 })?;
Andrew Walbrandff3b942021-06-09 15:20:36 +0000468
Jiyong Park9dd389e2021-08-23 20:42:59 +0900469 match partition_type {
470 PartitionType::RAW => Ok(()),
471 PartitionType::ANDROID_VM_INSTANCE => format_as_android_vm_instance(&mut part),
Shikha Panwar9fd198f2022-11-18 17:43:43 +0000472 PartitionType::ENCRYPTEDSTORE => format_as_encryptedstore(&mut part),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900473 _ => Err(Error::new(
474 ErrorKind::Unsupported,
475 format!("Unsupported partition type {:?}", partition_type),
476 )),
477 }
478 .map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000479 Status::new_service_specific_error_str(
480 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100481 Some(format!("Failed to initialize partition as {:?}: {:?}", partition_type, e)),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900482 )
483 })?;
484
Andrew Walbrandff3b942021-06-09 15:20:36 +0000485 Ok(())
486 }
487
Jiyong Park0a248432021-08-20 23:32:39 +0900488 /// Creates or update the idsig file by digesting the input APK file.
489 fn createOrUpdateIdsigFile(
490 &self,
491 input_fd: &ParcelFileDescriptor,
492 idsig_fd: &ParcelFileDescriptor,
493 ) -> binder::Result<()> {
494 // TODO(b/193504400): do this only when (1) idsig_fd is empty or (2) the APK digest in
495 // idsig_fd is different from APK digest in input_fd
496
Jiyong Parkc3ca24f2022-06-28 10:45:15 +0900497 check_manage_access()?;
498
Nikita Ioffef1ce9872022-12-09 13:31:59 +0000499 create_or_update_idsig_file(input_fd, idsig_fd)
500 .map_err(|e| Status::new_service_specific_error_str(-1, Some(format!("{:?}", e))))?;
Jiyong Park0a248432021-08-20 23:32:39 +0900501 Ok(())
502 }
503
Andrew Walbran320b5602021-03-04 16:11:12 +0000504 /// Get a list of all currently running VMs. This method is only intended for debug purposes,
505 /// and as such is only permitted from the shell user.
506 fn debugListVms(&self) -> binder::Result<Vec<VirtualMachineDebugInfo>> {
David Brazdil209074a2023-01-12 16:44:51 +0000507 // Delegate to the global service, including checking the debug permission.
David Brazdild4f51a52023-01-11 14:09:27 +0000508 GLOBAL_SERVICE.debugListVms()
Andrew Walbran320b5602021-03-04 16:11:12 +0000509 }
510}
511
Shikha Panwar7afc1392022-03-24 08:54:43 +0000512fn handle_stream_connection_tombstoned() -> Result<()> {
David Brazdil73988ea2022-11-11 15:10:32 +0000513 // Should not listen for tombstones on a guest VM's port.
514 assert!(!is_valid_guest_cid(VM_TOMBSTONES_SERVICE_PORT as Cid));
Shikha Panwar7afc1392022-03-24 08:54:43 +0000515 let listener =
David Brazdil73988ea2022-11-11 15:10:32 +0000516 VsockListener::bind_with_cid_port(VMADDR_CID_HOST, VM_TOMBSTONES_SERVICE_PORT as Cid)?;
Shikha Panwar7afc1392022-03-24 08:54:43 +0000517 for incoming_stream in listener.incoming() {
518 let mut incoming_stream = match incoming_stream {
519 Err(e) => {
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100520 warn!("invalid incoming connection: {:?}", e);
Shikha Panwar7afc1392022-03-24 08:54:43 +0000521 continue;
522 }
523 Ok(s) => s,
524 };
525 std::thread::spawn(move || {
526 if let Err(e) = handle_tombstone(&mut incoming_stream) {
527 error!("Failed to write tombstone- {:?}", e);
528 }
529 });
530 }
531 Ok(())
532}
533
534fn handle_tombstone(stream: &mut VsockStream) -> Result<()> {
Andrew Walbranadd38cb2022-10-06 17:01:03 +0000535 if let Ok(addr) = stream.peer_addr() {
Shikha Panwar7afc1392022-03-24 08:54:43 +0000536 info!("Vsock Stream connected to cid={} for tombstones", addr.cid());
537 }
538 let tb_connection =
539 TombstonedConnection::connect(std::process::id() as i32, DebuggerdDumpType::Tombstone)
540 .context("Failed to connect to tombstoned")?;
541 let mut text_output = tb_connection
542 .text_output
543 .as_ref()
544 .ok_or_else(|| anyhow!("Could not get file to write the tombstones on"))?;
545 let mut num_bytes_read = 0;
546 loop {
547 let mut chunk_recv = [0; CHUNK_RECV_MAX_LEN];
548 let n = stream
549 .read(&mut chunk_recv)
550 .context("Failed to read tombstone data from Vsock stream")?;
551 if n == 0 {
552 break;
553 }
554 num_bytes_read += n;
555 text_output.write_all(&chunk_recv[0..n]).context("Failed to write guests tombstones")?;
556 }
557 info!("Received {} bytes from guest & wrote to tombstone file", num_bytes_read);
558 tb_connection.notify_completion()?;
559 Ok(())
560}
561
Jiyong Park8611a6c2021-07-09 18:17:44 +0900562impl VirtualizationService {
David Brazdil4b4c5102022-12-19 22:56:20 +0000563 // TODO(b/245727626): Remove after the source files for virtualizationservice
564 // and virtmgr binaries are split from each other.
565 #[allow(dead_code)]
Jiyong Park8611a6c2021-07-09 18:17:44 +0900566 pub fn init() -> VirtualizationService {
David Brazdil49f96f52022-12-16 21:29:13 +0000567 VirtualizationService::default()
Jiyong Park8611a6c2021-07-09 18:17:44 +0900568 }
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000569
David Brazdil209074a2023-01-12 16:44:51 +0000570 fn create_vm_context(
571 &self,
572 requester_debug_pid: pid_t,
573 ) -> binder::Result<(VmContext, Cid, PathBuf)> {
David Brazdil8cf8f482022-11-23 14:21:26 +0000574 const NUM_ATTEMPTS: usize = 5;
575
576 for _ in 0..NUM_ATTEMPTS {
David Brazdild4f51a52023-01-11 14:09:27 +0000577 let vm_context = GLOBAL_SERVICE.allocateGlobalVmContext(requester_debug_pid as i32)?;
578 let cid = vm_context.getCid()? as Cid;
579 let temp_dir: PathBuf = vm_context.getTemporaryDirectory()?.into();
David Brazdil8cf8f482022-11-23 14:21:26 +0000580 let service = VirtualMachineService::new_binder(self.state.clone(), cid).as_binder();
581
582 // Start VM service listening for connections from the new CID on port=CID.
David Brazdil8cf8f482022-11-23 14:21:26 +0000583 let port = cid;
David Brazdil3238da42022-11-18 10:04:51 +0000584 match RpcServer::new_vsock(service, cid, port) {
David Brazdil8cf8f482022-11-23 14:21:26 +0000585 Ok(vm_server) => {
586 vm_server.start();
David Brazdild4f51a52023-01-11 14:09:27 +0000587 return Ok((VmContext::new(vm_context, vm_server), cid, temp_dir));
David Brazdil8cf8f482022-11-23 14:21:26 +0000588 }
589 Err(err) => {
590 warn!("Could not start RpcServer on port {}: {}", port, err);
591 }
592 }
593 }
David Brazdil209074a2023-01-12 16:44:51 +0000594 Err(Status::new_service_specific_error_str(
595 -1,
596 Some("Too many attempts to create VM context failed."),
597 ))
David Brazdil8cf8f482022-11-23 14:21:26 +0000598 }
599
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000600 fn create_vm_internal(
601 &self,
602 config: &VirtualMachineConfig,
603 console_fd: Option<&ParcelFileDescriptor>,
604 log_fd: Option<&ParcelFileDescriptor>,
605 is_protected: &mut bool,
606 ) -> binder::Result<Strong<dyn IVirtualMachine>> {
David Brazdil209074a2023-01-12 16:44:51 +0000607 let requester_uid = get_calling_uid();
608 let requester_debug_pid = get_calling_pid();
609
610 // Allocating VM context checks the MANAGE_VIRTUAL_MACHINE permission.
611 let (vm_context, cid, temporary_directory) = self.create_vm_context(requester_debug_pid)?;
Inseob Kim1119d702022-05-02 18:01:58 +0900612
Alan Stokes7bc146c2022-10-20 17:10:32 +0100613 let is_custom = match config {
614 VirtualMachineConfig::RawConfig(_) => true,
615 VirtualMachineConfig::AppConfig(config) => {
616 // Some features are reserved for platform apps only, even when using
617 // VirtualMachineAppConfig:
618 // - controlling CPUs;
619 // - specifying a config file in the APK.
620 !config.taskProfiles.is_empty() || matches!(config.payload, Payload::ConfigPath(_))
Inseob Kim1119d702022-05-02 18:01:58 +0900621 }
Alan Stokes7bc146c2022-10-20 17:10:32 +0100622 };
623 if is_custom {
624 check_use_custom_virtual_machine()?;
Inseob Kim1119d702022-05-02 18:01:58 +0900625 }
626
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000627 let state = &mut *self.state.lock().unwrap();
Inseob Kim0168b462022-12-27 14:54:35 +0900628 let console_fd =
629 clone_or_prepare_logger_fd(config, console_fd, format!("Console({})", cid))?;
630 let log_fd = clone_or_prepare_logger_fd(config, log_fd, format!("Log({})", cid))?;
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000631
632 // Counter to generate unique IDs for temporary image files.
633 let mut next_temporary_image_id = 0;
634 // Files which are referred to from composite images. These must be mapped to the crosvm
635 // child process, and not closed before it is started.
636 let mut indirect_files = vec![];
637
Alan Stokes7bc146c2022-10-20 17:10:32 +0100638 let (is_app_config, config) = match config {
639 VirtualMachineConfig::RawConfig(config) => (false, BorrowedOrOwned::Borrowed(config)),
640 VirtualMachineConfig::AppConfig(config) => {
641 let config = load_app_config(config, &temporary_directory).map_err(|e| {
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000642 *is_protected = config.protectedVm;
Alan Stokes0d1ef782022-09-27 13:46:35 +0100643 let message = format!("Failed to load app config: {:?}", e);
644 error!("{}", message);
645 Status::new_service_specific_error_str(-1, Some(message))
Alan Stokes7bc146c2022-10-20 17:10:32 +0100646 })?;
647 (true, BorrowedOrOwned::Owned(config))
648 }
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000649 };
650 let config = config.as_ref();
651 *is_protected = config.protectedVm;
652
653 // Check if partition images are labeled incorrectly. This is to prevent random images
654 // which are not protected by the Android Verified Boot (e.g. bits downloaded by apps) from
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100655 // being loaded in a pVM. This applies to everything in the raw config, and everything but
656 // the non-executable, generated partitions in the app config.
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000657 config
658 .disks
659 .iter()
660 .flat_map(|disk| disk.partitions.iter())
661 .filter(|partition| {
662 if is_app_config {
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100663 !is_safe_app_partition(&partition.label)
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000664 } else {
665 true // all partitions are checked
666 }
667 })
668 .try_for_each(check_label_for_partition)
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100669 .map_err(|e| Status::new_service_specific_error_str(-1, Some(format!("{:?}", e))))?;
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000670
Alan Stokes185fe112023-01-10 16:20:55 +0000671 let kernel = maybe_clone_file(&config.kernel)?;
672 let initrd = maybe_clone_file(&config.initrd)?;
673
674 // In a protected VM, we require custom kernels to come from a trusted source (b/237054515).
675 if config.protectedVm {
676 check_label_for_kernel_files(&kernel, &initrd).map_err(|e| {
677 Status::new_service_specific_error_str(-1, Some(format!("{:?}", e)))
678 })?;
679 }
680
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000681 let zero_filler_path = temporary_directory.join("zero.img");
682 write_zero_filler(&zero_filler_path).map_err(|e| {
Alan Stokes70ccf162022-07-08 11:05:03 +0100683 error!("Failed to make composite image: {:?}", e);
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000684 Status::new_service_specific_error_str(
685 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100686 Some(format!("Failed to make composite image: {:?}", e)),
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000687 )
688 })?;
689
690 // Assemble disk images if needed.
691 let disks = config
692 .disks
693 .iter()
694 .map(|disk| {
695 assemble_disk_image(
696 disk,
697 &zero_filler_path,
698 &temporary_directory,
699 &mut next_temporary_image_id,
700 &mut indirect_files,
701 )
702 })
703 .collect::<Result<Vec<DiskFile>, _>>()?;
704
Jiyong Parke558ab12022-07-07 20:18:55 +0900705 // Creating this ramdump file unconditionally is not harmful as ramdump will be created
706 // only when the VM is configured as such. `ramdump_write` is sent to crosvm and will
Jiyong Park4afe2012022-07-08 05:38:49 +0900707 // be the backing store for the /dev/hvc1 where VM will emit ramdump to. `ramdump_read`
Jiyong Parke558ab12022-07-07 20:18:55 +0900708 // will be sent back to the client (i.e. the VM owner) for readout.
709 let ramdump_path = temporary_directory.join("ramdump");
710 let ramdump = prepare_ramdump_file(&ramdump_path).map_err(|e| {
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100711 error!("Failed to prepare ramdump file: {:?}", e);
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000712 Status::new_service_specific_error_str(
713 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100714 Some(format!("Failed to prepare ramdump file: {:?}", e)),
Jiyong Parke558ab12022-07-07 20:18:55 +0900715 )
716 })?;
717
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000718 // Actually start the VM.
719 let crosvm_config = CrosvmConfig {
720 cid,
Seungjae Yoo62085c02022-08-12 04:44:52 +0000721 name: config.name.clone(),
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000722 bootloader: maybe_clone_file(&config.bootloader)?,
Alan Stokes185fe112023-01-10 16:20:55 +0000723 kernel,
724 initrd,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000725 disks,
726 params: config.params.to_owned(),
727 protected: *is_protected,
728 memory_mib: config.memoryMib.try_into().ok().and_then(NonZeroU32::new),
729 cpus: config.numCpus.try_into().ok().and_then(NonZeroU32::new),
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900730 task_profiles: config.taskProfiles.clone(),
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000731 console_fd,
732 log_fd,
Jiyong Parke558ab12022-07-07 20:18:55 +0900733 ramdump: Some(ramdump),
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000734 indirect_files,
735 platform_version: parse_platform_version_req(&config.platformVersion)?,
Jiyong Parke6ed0f92022-06-22 00:13:00 +0900736 detect_hangup: is_app_config,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000737 };
738 let instance = Arc::new(
David Brazdil528e0472022-10-10 15:06:02 +0100739 VmInstance::new(
740 crosvm_config,
741 temporary_directory,
742 requester_uid,
743 requester_debug_pid,
744 vm_context,
745 )
746 .map_err(|e| {
747 error!("Failed to create VM with config {:?}: {:?}", config, e);
748 Status::new_service_specific_error_str(
749 -1,
750 Some(format!("Failed to create VM: {:?}", e)),
751 )
752 })?,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000753 );
754 state.add_vm(Arc::downgrade(&instance));
755 Ok(VirtualMachine::create(instance))
756 }
Jiyong Park8611a6c2021-07-09 18:17:44 +0900757}
758
Andrew Walbranfbb39d22021-07-28 17:01:25 +0000759fn write_zero_filler(zero_filler_path: &Path) -> Result<()> {
Jooyung Han95884632021-07-06 22:27:54 +0900760 let file = OpenOptions::new()
761 .create_new(true)
762 .read(true)
763 .write(true)
764 .open(zero_filler_path)
765 .with_context(|| "Failed to create zero.img")?;
766 file.set_len(ZERO_FILLER_SIZE)?;
Andrew Walbranfbb39d22021-07-28 17:01:25 +0000767 Ok(())
Jooyung Han95884632021-07-06 22:27:54 +0900768}
769
Jiyong Park9dd389e2021-08-23 20:42:59 +0900770fn format_as_android_vm_instance(part: &mut dyn Write) -> std::io::Result<()> {
771 part.write_all(ANDROID_VM_INSTANCE_MAGIC.as_bytes())?;
772 part.write_all(&ANDROID_VM_INSTANCE_VERSION.to_le_bytes())?;
773 part.flush()
774}
775
Shikha Panwar9fd198f2022-11-18 17:43:43 +0000776fn format_as_encryptedstore(part: &mut dyn Write) -> std::io::Result<()> {
777 part.write_all(UNFORMATTED_STORAGE_MAGIC.as_bytes())?;
778 part.flush()
779}
780
Jiyong Parke558ab12022-07-07 20:18:55 +0900781fn prepare_ramdump_file(ramdump_path: &Path) -> Result<File> {
Chris Wailes9b866f02022-11-16 15:17:16 -0800782 File::create(ramdump_path).context(format!("Failed to create ramdump file {:?}", &ramdump_path))
Jiyong Parke558ab12022-07-07 20:18:55 +0900783}
784
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000785/// Given the configuration for a disk image, assembles the `DiskFile` to pass to crosvm.
786///
787/// This may involve assembling a composite disk from a set of partition images.
788fn assemble_disk_image(
789 disk: &DiskImage,
Jooyung Han95884632021-07-06 22:27:54 +0900790 zero_filler_path: &Path,
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000791 temporary_directory: &Path,
792 next_temporary_image_id: &mut u64,
793 indirect_files: &mut Vec<File>,
Andrew Walbran806f1542021-06-10 14:07:12 +0000794) -> Result<DiskFile, Status> {
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000795 let image = if !disk.partitions.is_empty() {
796 if disk.image.is_some() {
797 warn!("DiskImage {:?} contains both image and partitions.", disk);
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000798 return Err(Status::new_exception_str(
Andrew Walbran806f1542021-06-10 14:07:12 +0000799 ExceptionCode::ILLEGAL_ARGUMENT,
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000800 Some("DiskImage contains both image and partitions."),
Andrew Walbran806f1542021-06-10 14:07:12 +0000801 ));
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000802 }
803
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000804 let composite_image_filenames =
805 make_composite_image_filenames(temporary_directory, next_temporary_image_id);
806 let (image, partition_files) = make_composite_image(
807 &disk.partitions,
Jooyung Han95884632021-07-06 22:27:54 +0900808 zero_filler_path,
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000809 &composite_image_filenames.composite,
810 &composite_image_filenames.header,
811 &composite_image_filenames.footer,
812 )
813 .map_err(|e| {
Alan Stokes70ccf162022-07-08 11:05:03 +0100814 error!("Failed to make composite image with config {:?}: {:?}", disk, e);
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000815 Status::new_service_specific_error_str(
816 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100817 Some(format!("Failed to make composite image: {:?}", e)),
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000818 )
819 })?;
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000820
821 // Pass the file descriptors for the various partition files to crosvm when it
822 // is run.
823 indirect_files.extend(partition_files);
824
825 image
826 } else if let Some(image) = &disk.image {
827 clone_file(image)?
828 } else {
829 warn!("DiskImage {:?} didn't contain image or partitions.", disk);
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000830 return Err(Status::new_exception_str(
Andrew Walbran806f1542021-06-10 14:07:12 +0000831 ExceptionCode::ILLEGAL_ARGUMENT,
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000832 Some("DiskImage didn't contain image or partitions."),
Andrew Walbran806f1542021-06-10 14:07:12 +0000833 ));
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000834 };
835
836 Ok(DiskFile { image, writable: disk.writable })
837}
838
Jooyung Han21e9b922021-06-26 04:14:16 +0900839fn load_app_config(
840 config: &VirtualMachineAppConfig,
841 temporary_directory: &Path,
Jooyung Hanadfb76c2021-06-28 17:29:30 +0900842) -> Result<VirtualMachineRawConfig> {
Andrew Walbrancc0db522021-07-12 17:03:42 +0000843 let apk_file = clone_file(config.apk.as_ref().unwrap())?;
844 let idsig_file = clone_file(config.idsig.as_ref().unwrap())?;
Jiyong Park8d081812021-07-23 17:45:04 +0900845 let instance_file = clone_file(config.instanceImage.as_ref().unwrap())?;
Jooyung Han21e9b922021-06-26 04:14:16 +0900846
Shikha Panwar22e70452022-10-10 18:32:55 +0000847 let storage_image = if let Some(file) = config.encryptedStorageImage.as_ref() {
848 Some(clone_file(file)?)
849 } else {
850 None
851 };
852
Alan Stokes0d1ef782022-09-27 13:46:35 +0100853 let vm_payload_config = match &config.payload {
854 Payload::ConfigPath(config_path) => {
855 load_vm_payload_config_from_file(&apk_file, config_path.as_str())
856 .with_context(|| format!("Couldn't read config from {}", config_path))?
857 }
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000858 Payload::PayloadConfig(payload_config) => create_vm_payload_config(payload_config)?,
Alan Stokes0d1ef782022-09-27 13:46:35 +0100859 };
Jooyung Han21e9b922021-06-26 04:14:16 +0900860
Alan Stokes0d1ef782022-09-27 13:46:35 +0100861 // For now, the only supported OS is Microdroid
862 let os_name = vm_payload_config.os.name.as_str();
863 if os_name != MICRODROID_OS_NAME {
Andrew Walbrancc0db522021-07-12 17:03:42 +0000864 bail!("Unknown OS \"{}\"", os_name);
Jooyung Han35edb8f2021-07-01 16:17:16 +0900865 }
Andrew Walbrancc0db522021-07-12 17:03:42 +0000866
867 // It is safe to construct a filename based on the os_name because we've already checked that it
868 // is one of the allowed values.
Jooyung Han21e9b922021-06-26 04:14:16 +0900869 let vm_config_path = PathBuf::from(format!("/apex/com.android.virt/etc/{}.json", os_name));
870 let vm_config_file = File::open(vm_config_path)?;
Andrew Walbrancc0db522021-07-12 17:03:42 +0000871 let mut vm_config = VmConfig::load(&vm_config_file)?.to_parcelable()?;
Jooyung Han21e9b922021-06-26 04:14:16 +0900872
Andrew Walbrancc045902021-07-27 16:06:17 +0000873 if config.memoryMib > 0 {
874 vm_config.memoryMib = config.memoryMib;
Andrew Walbran45bcb0c2021-07-14 15:02:06 +0000875 }
876
Seungjae Yoo62085c02022-08-12 04:44:52 +0000877 vm_config.name = config.name.clone();
Andrew Walbran3994f002022-01-27 17:33:45 +0000878 vm_config.protectedVm = config.protectedVm;
Jiyong Park032615f2022-01-10 13:55:34 +0900879 vm_config.numCpus = config.numCpus;
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900880 vm_config.taskProfiles = config.taskProfiles.clone();
Jiyong Park032615f2022-01-10 13:55:34 +0900881
Shikha Panwar22e70452022-10-10 18:32:55 +0000882 // Microdroid takes additional init ramdisk & (optionally) storage image
883 add_microdroid_system_images(config, instance_file, storage_image, &mut vm_config)?;
884
885 // Include Microdroid payload disk (contains apks, idsigs) in vm config
886 add_microdroid_payload_images(
Alan Stokes0d1ef782022-09-27 13:46:35 +0100887 config,
888 temporary_directory,
889 apk_file,
890 idsig_file,
Alan Stokes0d1ef782022-09-27 13:46:35 +0100891 &vm_payload_config,
892 &mut vm_config,
893 )?;
Jooyung Han21e9b922021-06-26 04:14:16 +0900894
Andrew Walbrancc0db522021-07-12 17:03:42 +0000895 Ok(vm_config)
Jooyung Han21e9b922021-06-26 04:14:16 +0900896}
897
Alan Stokes0d1ef782022-09-27 13:46:35 +0100898fn load_vm_payload_config_from_file(apk_file: &File, config_path: &str) -> Result<VmPayloadConfig> {
899 let mut apk_zip = ZipArchive::new(apk_file)?;
900 let config_file = apk_zip.by_name(config_path)?;
901 Ok(serde_json::from_reader(config_file)?)
902}
903
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000904fn create_vm_payload_config(
905 payload_config: &VirtualMachinePayloadConfig,
906) -> Result<VmPayloadConfig> {
Alan Stokes0d1ef782022-09-27 13:46:35 +0100907 // There isn't an actual config file. Construct a synthetic VmPayloadConfig from the explicit
908 // parameters we've been given. Microdroid will do something equivalent inside the VM using the
909 // payload config that we send it via the metadata file.
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000910
911 let payload_binary_name = &payload_config.payloadBinaryName;
912 if payload_binary_name.contains('/') {
913 bail!("Payload binary name must not specify a path: {payload_binary_name}");
914 }
915
916 let task = Task { type_: TaskType::MicrodroidLauncher, command: payload_binary_name.clone() };
917 Ok(VmPayloadConfig {
Alan Stokes0d1ef782022-09-27 13:46:35 +0100918 os: OsConfig { name: MICRODROID_OS_NAME.to_owned() },
919 task: Some(task),
920 apexes: vec![],
921 extra_apks: vec![],
922 prefer_staged: false,
Alan Stokes1f417c92022-09-29 15:13:28 +0100923 export_tombstones: false,
Alan Stokes0d1ef782022-09-27 13:46:35 +0100924 enable_authfs: false,
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000925 })
Alan Stokes0d1ef782022-09-27 13:46:35 +0100926}
927
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000928/// Generates a unique filename to use for a composite disk image.
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000929fn make_composite_image_filenames(
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000930 temporary_directory: &Path,
931 next_temporary_image_id: &mut u64,
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000932) -> CompositeImageFilenames {
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000933 let id = *next_temporary_image_id;
934 *next_temporary_image_id += 1;
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000935 CompositeImageFilenames {
936 composite: temporary_directory.join(format!("composite-{}.img", id)),
937 header: temporary_directory.join(format!("composite-{}-header.img", id)),
938 footer: temporary_directory.join(format!("composite-{}-footer.img", id)),
939 }
940}
941
942/// Filenames for a composite disk image, including header and footer partitions.
943#[derive(Clone, Debug, Eq, PartialEq)]
944struct CompositeImageFilenames {
945 /// The composite disk image itself.
946 composite: PathBuf,
947 /// The header partition image.
948 header: PathBuf,
949 /// The footer partition image.
950 footer: PathBuf,
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000951}
952
Jiyong Park753553b2021-07-12 21:21:09 +0900953/// Checks whether the caller has a specific permission
954fn check_permission(perm: &str) -> binder::Result<()> {
David Brazdil1f530702022-10-03 12:18:10 +0100955 let calling_pid = get_calling_pid();
956 let calling_uid = get_calling_uid();
Jiyong Park753553b2021-07-12 21:21:09 +0900957 // Root can do anything
958 if calling_uid == 0 {
959 return Ok(());
960 }
961 let perm_svc: Strong<dyn IPermissionController::IPermissionController> =
962 binder::get_interface("permission")?;
963 if perm_svc.checkPermission(perm, calling_pid, calling_uid as i32)? {
Andrew Walbran806f1542021-06-10 14:07:12 +0000964 Ok(())
965 } else {
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000966 Err(Status::new_exception_str(
Jiyong Park753553b2021-07-12 21:21:09 +0900967 ExceptionCode::SECURITY,
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000968 Some(format!("does not have the {} permission", perm)),
Jiyong Park753553b2021-07-12 21:21:09 +0900969 ))
Andrew Walbran806f1542021-06-10 14:07:12 +0000970 }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000971}
972
Jiyong Park753553b2021-07-12 21:21:09 +0900973/// Check whether the caller of the current Binder method is allowed to call debug methods.
974fn check_debug_access() -> binder::Result<()> {
975 check_permission("android.permission.DEBUG_VIRTUAL_MACHINE")
976}
977
978/// Check whether the caller of the current Binder method is allowed to manage VMs
979fn check_manage_access() -> binder::Result<()> {
980 check_permission("android.permission.MANAGE_VIRTUAL_MACHINE")
981}
982
Inseob Kim1119d702022-05-02 18:01:58 +0900983/// Check whether the caller of the current Binder method is allowed to create custom VMs
984fn check_use_custom_virtual_machine() -> binder::Result<()> {
985 check_permission("android.permission.USE_CUSTOM_VIRTUAL_MACHINE")
986}
987
Alan Stokes185fe112023-01-10 16:20:55 +0000988/// Return whether a partition is exempt from selinux label checks, because we know that it does
989/// not contain code and is likely to be generated in an app-writable directory.
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100990fn is_safe_app_partition(label: &str) -> bool {
Shikha Panwara2ff8c52022-11-30 19:25:46 +0000991 // See add_microdroid_system_images & add_microdroid_payload_images in payload.rs.
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100992 label == "vm-instance"
Shikha Panwara2ff8c52022-11-30 19:25:46 +0000993 || label == "encryptedstore"
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100994 || label == "microdroid-apk-idsig"
995 || label == "payload-metadata"
996 || label.starts_with("extra-idsig-")
997}
998
Alan Stokes185fe112023-01-10 16:20:55 +0000999/// Check that a file SELinux label is acceptable.
1000///
1001/// We only want to allow code in a VM to be sourced from places that apps, and the
1002/// system, do not have write access to.
1003///
1004/// Note that sepolicy must also grant read access for these types to both virtualization
1005/// service and crosvm.
1006///
1007/// App private data files are deliberately excluded, to avoid arbitrary payloads being run on
1008/// user devices (W^X).
1009fn check_label_is_allowed(context: &SeContext) -> Result<()> {
1010 match context.selinux_type()? {
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001011 | "system_file" // immutable dm-verity protected partition
1012 | "apk_data_file" // APKs of an installed app
Shikha Panwara2ff8c52022-11-30 19:25:46 +00001013 | "staging_data_file" // updated/staged APEX images
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001014 | "shell_data_file" // test files created via adb shell
1015 => Ok(()),
Alan Stokes185fe112023-01-10 16:20:55 +00001016 _ => bail!("Label {} is not allowed", context),
Jiyong Park029977d2021-11-24 21:56:49 +09001017 }
1018}
1019
Alan Stokes185fe112023-01-10 16:20:55 +00001020fn check_label_for_partition(partition: &Partition) -> Result<()> {
1021 let file = partition.image.as_ref().unwrap().as_ref();
1022 check_label_is_allowed(&getfilecon(file)?)
1023 .with_context(|| format!("Partition {} invalid", &partition.label))
1024}
1025
1026fn check_label_for_kernel_files(kernel: &Option<File>, initrd: &Option<File>) -> Result<()> {
1027 if let Some(f) = kernel {
1028 check_label_for_file(f, "kernel")?;
1029 }
1030 if let Some(f) = initrd {
1031 check_label_for_file(f, "initrd")?;
1032 }
1033 Ok(())
1034}
1035fn check_label_for_file(file: &File, name: &str) -> Result<()> {
1036 check_label_is_allowed(&getfilecon(file)?).with_context(|| format!("{} file invalid", name))
1037}
1038
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001039/// Implementation of the AIDL `IVirtualMachine` interface. Used as a handle to a VM.
1040#[derive(Debug)]
1041struct VirtualMachine {
1042 instance: Arc<VmInstance>,
1043}
1044
1045impl VirtualMachine {
1046 fn create(instance: Arc<VmInstance>) -> Strong<dyn IVirtualMachine> {
David Brazdil4b4c5102022-12-19 22:56:20 +00001047 BnVirtualMachine::new_binder(VirtualMachine { instance }, BinderFeatures::default())
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001048 }
1049}
1050
1051impl Interface for VirtualMachine {}
1052
1053impl IVirtualMachine for VirtualMachine {
1054 fn getCid(&self) -> binder::Result<i32> {
Jiyong Park753553b2021-07-12 21:21:09 +09001055 // Don't check permission. The owner of the VM might have passed this binder object to
1056 // others.
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001057 Ok(self.instance.cid as i32)
1058 }
Andrew Walbrandae07162021-03-12 17:05:20 +00001059
Andrew Walbran6b650662021-09-07 13:13:23 +00001060 fn getState(&self) -> binder::Result<VirtualMachineState> {
Jiyong Park753553b2021-07-12 21:21:09 +09001061 // Don't check permission. The owner of the VM might have passed this binder object to
1062 // others.
Andrew Walbran6b650662021-09-07 13:13:23 +00001063 Ok(get_state(&self.instance))
Andrew Walbrandae07162021-03-12 17:05:20 +00001064 }
1065
1066 fn registerCallback(
1067 &self,
1068 callback: &Strong<dyn IVirtualMachineCallback>,
1069 ) -> binder::Result<()> {
Jiyong Park753553b2021-07-12 21:21:09 +09001070 // Don't check permission. The owner of the VM might have passed this binder object to
1071 // others.
1072 //
Andrew Walbrandae07162021-03-12 17:05:20 +00001073 // TODO: Should this give an error if the VM is already dead?
1074 self.instance.callbacks.add(callback.clone());
1075 Ok(())
1076 }
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001077
Andrew Walbranf8d94112021-09-07 11:45:36 +00001078 fn start(&self) -> binder::Result<()> {
1079 self.instance.start().map_err(|e| {
1080 error!("Error starting VM with CID {}: {:?}", self.instance.cid, e);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001081 Status::new_service_specific_error_str(-1, Some(e.to_string()))
Andrew Walbranf8d94112021-09-07 11:45:36 +00001082 })
1083 }
1084
Inseob Kima446f802022-07-11 19:46:37 +09001085 fn stop(&self) -> binder::Result<()> {
1086 self.instance.kill().map_err(|e| {
1087 error!("Error stopping VM with CID {}: {:?}", self.instance.cid, e);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001088 Status::new_service_specific_error_str(-1, Some(e.to_string()))
Inseob Kima446f802022-07-11 19:46:37 +09001089 })
1090 }
1091
Keir Frasercdd4b112022-11-24 14:02:25 +00001092 fn onTrimMemory(&self, level: MemoryTrimLevel) -> binder::Result<()> {
1093 self.instance.trim_memory(level).map_err(|e| {
1094 error!("Error trimming VM with CID {}: {:?}", self.instance.cid, e);
1095 Status::new_service_specific_error_str(-1, Some(e.to_string()))
1096 })
1097 }
1098
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001099 fn connectVsock(&self, port: i32) -> binder::Result<ParcelFileDescriptor> {
Andrew Walbranf8d94112021-09-07 11:45:36 +00001100 if !matches!(&*self.instance.vm_state.lock().unwrap(), VmState::Running { .. }) {
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001101 return Err(Status::new_service_specific_error_str(-1, Some("VM is not running")));
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001102 }
Alan Stokes10c47672022-12-13 17:17:08 +00001103 let port = port as u32;
1104 if port < 1024 {
1105 return Err(Status::new_service_specific_error_str(
1106 -1,
1107 Some(format!("Can't connect to privileged port {port}")),
1108 ));
1109 }
1110 let stream = VsockStream::connect_with_cid_port(self.instance.cid, port).map_err(|e| {
1111 Status::new_service_specific_error_str(-1, Some(format!("Failed to connect: {:?}", e)))
1112 })?;
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001113 Ok(vsock_stream_to_pfd(stream))
1114 }
Andrew Walbrandae07162021-03-12 17:05:20 +00001115}
1116
1117impl Drop for VirtualMachine {
1118 fn drop(&mut self) {
1119 debug!("Dropping {:?}", self);
Inseob Kima446f802022-07-11 19:46:37 +09001120 if let Err(e) = self.instance.kill() {
1121 debug!("Error stopping dropped VM with CID {}: {:?}", self.instance.cid, e);
1122 }
Andrew Walbrandae07162021-03-12 17:05:20 +00001123 }
1124}
1125
1126/// A set of Binders to be called back in response to various events on the VM, such as when it
1127/// dies.
1128#[derive(Debug, Default)]
1129pub struct VirtualMachineCallbacks(Mutex<Vec<Strong<dyn IVirtualMachineCallback>>>);
1130
1131impl VirtualMachineCallbacks {
Jiyong Park8611a6c2021-07-09 18:17:44 +09001132 /// Call all registered callbacks to notify that the payload has started.
David Brazdil451cc962022-10-14 14:08:12 +01001133 pub fn notify_payload_started(&self, cid: Cid) {
Jiyong Park8611a6c2021-07-09 18:17:44 +09001134 let callbacks = &*self.0.lock().unwrap();
Jiyong Park8611a6c2021-07-09 18:17:44 +09001135 for callback in callbacks {
David Brazdil451cc962022-10-14 14:08:12 +01001136 if let Err(e) = callback.onPayloadStarted(cid as i32) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001137 error!("Error notifying payload start event from VM CID {}: {:?}", cid, e);
Jiyong Park8611a6c2021-07-09 18:17:44 +09001138 }
1139 }
1140 }
1141
Inseob Kim14cb8692021-08-31 21:50:39 +09001142 /// Call all registered callbacks to notify that the payload is ready to serve.
1143 pub fn notify_payload_ready(&self, cid: Cid) {
1144 let callbacks = &*self.0.lock().unwrap();
1145 for callback in callbacks {
1146 if let Err(e) = callback.onPayloadReady(cid as i32) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001147 error!("Error notifying payload ready event from VM CID {}: {:?}", cid, e);
Inseob Kim14cb8692021-08-31 21:50:39 +09001148 }
1149 }
1150 }
1151
Inseob Kim2444af92021-08-31 01:22:50 +09001152 /// Call all registered callbacks to notify that the payload has finished.
1153 pub fn notify_payload_finished(&self, cid: Cid, exit_code: i32) {
1154 let callbacks = &*self.0.lock().unwrap();
1155 for callback in callbacks {
1156 if let Err(e) = callback.onPayloadFinished(cid as i32, exit_code) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001157 error!("Error notifying payload finish event from VM CID {}: {:?}", cid, e);
Inseob Kim2444af92021-08-31 01:22:50 +09001158 }
1159 }
1160 }
1161
Jooyung Handd0a1732021-11-23 15:26:20 +09001162 /// Call all registered callbacks to say that the VM encountered an error.
Alan Stokes2bead0d2022-09-05 16:58:34 +01001163 pub fn notify_error(&self, cid: Cid, error_code: ErrorCode, message: &str) {
Jooyung Handd0a1732021-11-23 15:26:20 +09001164 let callbacks = &*self.0.lock().unwrap();
1165 for callback in callbacks {
1166 if let Err(e) = callback.onError(cid as i32, error_code, message) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001167 error!("Error notifying error event from VM CID {}: {:?}", cid, e);
Jooyung Handd0a1732021-11-23 15:26:20 +09001168 }
1169 }
1170 }
1171
Andrew Walbrandae07162021-03-12 17:05:20 +00001172 /// Call all registered callbacks to say that the VM has died.
Andrew Walbranc92d35f2022-01-12 12:45:19 +00001173 pub fn callback_on_died(&self, cid: Cid, reason: DeathReason) {
Andrew Walbrandae07162021-03-12 17:05:20 +00001174 let callbacks = &*self.0.lock().unwrap();
1175 for callback in callbacks {
Andrew Walbranc92d35f2022-01-12 12:45:19 +00001176 if let Err(e) = callback.onDied(cid as i32, reason) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001177 error!("Error notifying exit of VM CID {}: {:?}", cid, e);
Andrew Walbrandae07162021-03-12 17:05:20 +00001178 }
1179 }
1180 }
1181
1182 /// Add a new callback to the set.
1183 fn add(&self, callback: Strong<dyn IVirtualMachineCallback>) {
1184 self.0.lock().unwrap().push(callback);
1185 }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001186}
1187
Andrew Walbranf6bf6862021-05-21 12:41:13 +00001188/// The mutable state of the VirtualizationService. There should only be one instance of this
1189/// struct.
Chris Wailes641fc4a2021-12-01 15:03:21 -08001190#[derive(Debug, Default)]
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001191struct State {
Andrew Walbran320b5602021-03-04 16:11:12 +00001192 /// The VMs which have been started. When VMs are started a weak reference is added to this list
1193 /// while a strong reference is returned to the caller over Binder. Once all copies of the
1194 /// Binder client are dropped the weak reference here will become invalid, and will be removed
1195 /// from the list opportunistically the next time `add_vm` is called.
1196 vms: Vec<Weak<VmInstance>>,
1197}
1198
1199impl State {
Andrew Walbrandae07162021-03-12 17:05:20 +00001200 /// Get a list of VMs which still have Binder references to them.
Andrew Walbran320b5602021-03-04 16:11:12 +00001201 fn vms(&self) -> Vec<Arc<VmInstance>> {
1202 // Attempt to upgrade the weak pointers to strong pointers.
1203 self.vms.iter().filter_map(Weak::upgrade).collect()
1204 }
1205
1206 /// Add a new VM to the list.
1207 fn add_vm(&mut self, vm: Weak<VmInstance>) {
1208 // Garbage collect any entries from the stored list which no longer exist.
1209 self.vms.retain(|vm| vm.strong_count() > 0);
1210
1211 // Actually add the new VM.
1212 self.vms.push(vm);
1213 }
David Brazdil3c2ddef2021-03-18 13:09:57 +00001214
Jiyong Park8611a6c2021-07-09 18:17:44 +09001215 /// Get a VM that corresponds to the given cid
1216 fn get_vm(&self, cid: Cid) -> Option<Arc<VmInstance>> {
1217 self.vms().into_iter().find(|vm| vm.cid == cid)
1218 }
Jiyong Parkd50a0242021-09-16 21:00:14 +09001219}
1220
Andrew Walbran6b650662021-09-07 13:13:23 +00001221/// Gets the `VirtualMachineState` of the given `VmInstance`.
1222fn get_state(instance: &VmInstance) -> VirtualMachineState {
Andrew Walbranf8d94112021-09-07 11:45:36 +00001223 match &*instance.vm_state.lock().unwrap() {
1224 VmState::NotStarted { .. } => VirtualMachineState::NOT_STARTED,
1225 VmState::Running { .. } => match instance.payload_state() {
Andrew Walbran6b650662021-09-07 13:13:23 +00001226 PayloadState::Starting => VirtualMachineState::STARTING,
1227 PayloadState::Started => VirtualMachineState::STARTED,
1228 PayloadState::Ready => VirtualMachineState::READY,
1229 PayloadState::Finished => VirtualMachineState::FINISHED,
Jiyong Parka4eebde2022-07-12 18:01:12 +09001230 PayloadState::Hangup => VirtualMachineState::DEAD,
Andrew Walbranf8d94112021-09-07 11:45:36 +00001231 },
1232 VmState::Dead => VirtualMachineState::DEAD,
1233 VmState::Failed => VirtualMachineState::DEAD,
Andrew Walbran6b650662021-09-07 13:13:23 +00001234 }
1235}
1236
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +00001237/// Converts a `&ParcelFileDescriptor` to a `File` by cloning the file.
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +00001238pub fn clone_file(file: &ParcelFileDescriptor) -> Result<File, Status> {
Andrew Walbran806f1542021-06-10 14:07:12 +00001239 file.as_ref().try_clone().map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001240 Status::new_exception_str(
Andrew Walbran806f1542021-06-10 14:07:12 +00001241 ExceptionCode::BAD_PARCELABLE,
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001242 Some(format!("Failed to clone File from ParcelFileDescriptor: {:?}", e)),
Andrew Walbran806f1542021-06-10 14:07:12 +00001243 )
1244 })
1245}
1246
Andrew Walbrand3a84182021-09-07 14:48:52 +00001247/// Converts an `&Option<ParcelFileDescriptor>` to an `Option<File>` by cloning the file.
1248fn maybe_clone_file(file: &Option<ParcelFileDescriptor>) -> Result<Option<File>, Status> {
1249 file.as_ref().map(clone_file).transpose()
1250}
1251
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001252/// Converts a `VsockStream` to a `ParcelFileDescriptor`.
1253fn vsock_stream_to_pfd(stream: VsockStream) -> ParcelFileDescriptor {
1254 // SAFETY: ownership is transferred from stream to f
1255 let f = unsafe { File::from_raw_fd(stream.into_raw_fd()) };
1256 ParcelFileDescriptor::new(f)
1257}
1258
Jiyong Parkdcf17412022-02-08 15:07:23 +09001259/// Parses the platform version requirement string.
1260fn parse_platform_version_req(s: &str) -> Result<VersionReq, Status> {
1261 VersionReq::parse(s).map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001262 Status::new_exception_str(
Jiyong Parkdcf17412022-02-08 15:07:23 +09001263 ExceptionCode::BAD_PARCELABLE,
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001264 Some(format!("Invalid platform version requirement {}: {:?}", s, e)),
Jiyong Parkdcf17412022-02-08 15:07:23 +09001265 )
1266 })
1267}
1268
Inseob Kim0168b462022-12-27 14:54:35 +09001269fn is_debuggable(config: &VirtualMachineConfig) -> bool {
1270 match config {
1271 VirtualMachineConfig::AppConfig(config) => config.debugLevel != DebugLevel::NONE,
1272 _ => false,
1273 }
1274}
1275
1276fn clone_or_prepare_logger_fd(
1277 config: &VirtualMachineConfig,
1278 fd: Option<&ParcelFileDescriptor>,
1279 tag: String,
1280) -> Result<Option<File>, Status> {
1281 if let Some(fd) = fd {
1282 return Ok(Some(clone_file(fd)?));
1283 }
1284
1285 if !is_debuggable(config) {
1286 return Ok(None);
1287 }
1288
1289 let (raw_read_fd, raw_write_fd) = pipe().map_err(|e| {
1290 Status::new_service_specific_error_str(-1, Some(format!("Failed to create pipe: {:?}", e)))
1291 })?;
1292
1293 // SAFETY: We are the sole owners of these fds as they were just created.
1294 let mut reader = BufReader::new(unsafe { File::from_raw_fd(raw_read_fd) });
1295 let write_fd = unsafe { File::from_raw_fd(raw_write_fd) };
1296
1297 std::thread::spawn(move || loop {
1298 let mut buf = vec![];
1299 match reader.read_until(b'\n', &mut buf) {
1300 Ok(0) => {
1301 // EOF
1302 return;
1303 }
1304 Ok(size) => {
1305 if buf[size - 1] == b'\n' {
1306 buf.pop();
1307 }
1308 info!("{}: {}", &tag, &String::from_utf8_lossy(&buf));
1309 }
1310 Err(e) => {
1311 error!("Could not read console pipe: {:?}", e);
1312 return;
1313 }
1314 };
1315 });
1316
1317 Ok(Some(write_fd))
1318}
1319
Jooyung Han35edb8f2021-07-01 16:17:16 +09001320/// Simple utility for referencing Borrowed or Owned. Similar to std::borrow::Cow, but
1321/// it doesn't require that T implements Clone.
1322enum BorrowedOrOwned<'a, T> {
1323 Borrowed(&'a T),
1324 Owned(T),
1325}
1326
1327impl<'a, T> AsRef<T> for BorrowedOrOwned<'a, T> {
1328 fn as_ref(&self) -> &T {
1329 match self {
1330 Self::Borrowed(b) => b,
Chris Wailes68c39f82021-07-27 16:03:44 -07001331 Self::Owned(o) => o,
Jooyung Han35edb8f2021-07-01 16:17:16 +09001332 }
1333 }
1334}
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001335
1336/// Implementation of `IVirtualMachineService`, the entry point of the AIDL service.
1337#[derive(Debug, Default)]
1338struct VirtualMachineService {
1339 state: Arc<Mutex<State>>,
Inseob Kimc7d28c72021-10-25 14:28:10 +00001340 cid: Cid,
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001341}
1342
1343impl Interface for VirtualMachineService {}
1344
1345impl IVirtualMachineService for VirtualMachineService {
Inseob Kimc7d28c72021-10-25 14:28:10 +00001346 fn notifyPayloadStarted(&self) -> binder::Result<()> {
1347 let cid = self.cid;
Inseob Kim7f61fe72021-08-20 20:50:47 +09001348 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001349 info!("VM with CID {} started payload", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001350 vm.update_payload_state(PayloadState::Started).map_err(|e| {
1351 Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
1352 })?;
David Brazdil451cc962022-10-14 14:08:12 +01001353 vm.callbacks.notify_payload_started(cid);
Seungjae Yoo62085c02022-08-12 04:44:52 +00001354
Seungjae Yoo6d265d92022-11-15 10:51:33 +09001355 let vm_start_timestamp = vm.vm_metric.lock().unwrap().start_timestamp;
1356 write_vm_booted_stats(vm.requester_uid as i32, &vm.name, vm_start_timestamp);
Inseob Kim7f61fe72021-08-20 20:50:47 +09001357 Ok(())
1358 } else {
Jooyung Handd0a1732021-11-23 15:26:20 +09001359 error!("notifyPayloadStarted is called from an unknown CID {}", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001360 Err(Status::new_service_specific_error_str(
1361 -1,
1362 Some(format!("cannot find a VM with CID {}", cid)),
Inseob Kim7f61fe72021-08-20 20:50:47 +09001363 ))
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001364 }
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001365 }
Inseob Kim2444af92021-08-31 01:22:50 +09001366
Inseob Kimc7d28c72021-10-25 14:28:10 +00001367 fn notifyPayloadReady(&self) -> binder::Result<()> {
1368 let cid = self.cid;
Inseob Kim14cb8692021-08-31 21:50:39 +09001369 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001370 info!("VM with CID {} reported payload is ready", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001371 vm.update_payload_state(PayloadState::Ready).map_err(|e| {
1372 Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
1373 })?;
Inseob Kim14cb8692021-08-31 21:50:39 +09001374 vm.callbacks.notify_payload_ready(cid);
1375 Ok(())
1376 } else {
Jooyung Handd0a1732021-11-23 15:26:20 +09001377 error!("notifyPayloadReady is called from an unknown CID {}", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001378 Err(Status::new_service_specific_error_str(
1379 -1,
1380 Some(format!("cannot find a VM with CID {}", cid)),
Inseob Kim14cb8692021-08-31 21:50:39 +09001381 ))
1382 }
1383 }
1384
Inseob Kimc7d28c72021-10-25 14:28:10 +00001385 fn notifyPayloadFinished(&self, exit_code: i32) -> binder::Result<()> {
1386 let cid = self.cid;
Inseob Kim2444af92021-08-31 01:22:50 +09001387 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001388 info!("VM with CID {} finished payload", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001389 vm.update_payload_state(PayloadState::Finished).map_err(|e| {
1390 Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
1391 })?;
Inseob Kim2444af92021-08-31 01:22:50 +09001392 vm.callbacks.notify_payload_finished(cid, exit_code);
1393 Ok(())
1394 } else {
Jooyung Handd0a1732021-11-23 15:26:20 +09001395 error!("notifyPayloadFinished is called from an unknown CID {}", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001396 Err(Status::new_service_specific_error_str(
1397 -1,
1398 Some(format!("cannot find a VM with CID {}", cid)),
Jooyung Handd0a1732021-11-23 15:26:20 +09001399 ))
1400 }
1401 }
1402
Alan Stokes2bead0d2022-09-05 16:58:34 +01001403 fn notifyError(&self, error_code: ErrorCode, message: &str) -> binder::Result<()> {
Jooyung Handd0a1732021-11-23 15:26:20 +09001404 let cid = self.cid;
1405 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001406 info!("VM with CID {} encountered an error", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001407 vm.update_payload_state(PayloadState::Finished).map_err(|e| {
1408 Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
1409 })?;
Jooyung Handd0a1732021-11-23 15:26:20 +09001410 vm.callbacks.notify_error(cid, error_code, message);
1411 Ok(())
1412 } else {
Seungjae Yooec8c1602022-06-20 05:28:00 +00001413 error!("notifyError is called from an unknown CID {}", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001414 Err(Status::new_service_specific_error_str(
1415 -1,
1416 Some(format!("cannot find a VM with CID {}", cid)),
Inseob Kim2444af92021-08-31 01:22:50 +09001417 ))
1418 }
1419 }
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001420}
1421
1422impl VirtualMachineService {
Inseob Kimc7d28c72021-10-25 14:28:10 +00001423 fn new_binder(state: Arc<Mutex<State>>, cid: Cid) -> Strong<dyn IVirtualMachineService> {
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001424 BnVirtualMachineService::new_binder(
Inseob Kimc7d28c72021-10-25 14:28:10 +00001425 VirtualMachineService { state, cid },
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001426 BinderFeatures::default(),
1427 )
1428 }
1429}
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001430
1431#[cfg(test)]
1432mod tests {
1433 use super::*;
1434
1435 #[test]
1436 fn test_is_allowed_label_for_partition() -> Result<()> {
1437 let expected_results = vec![
1438 ("u:object_r:system_file:s0", true),
1439 ("u:object_r:apk_data_file:s0", true),
1440 ("u:object_r:app_data_file:s0", false),
1441 ("u:object_r:app_data_file:s0:c512,c768", false),
1442 ("u:object_r:privapp_data_file:s0:c512,c768", false),
1443 ("invalid", false),
1444 ("user:role:apk_data_file:severity:categories", true),
1445 ("user:role:apk_data_file:severity:categories:extraneous", false),
1446 ];
1447
1448 for (label, expected_valid) in expected_results {
1449 let context = SeContext::new(label)?;
1450 let result = check_label_is_allowed(&context);
1451 if expected_valid {
1452 assert!(result.is_ok(), "Expected label {} to be allowed, got {:?}", label, result);
1453 } else if result.is_ok() {
1454 bail!("Expected label {} to be disallowed", label);
1455 }
1456 }
1457 Ok(())
1458 }
Nikita Ioffef1ce9872022-12-09 13:31:59 +00001459
1460 #[test]
1461 fn test_create_or_update_idsig_file_empty_apk() -> Result<()> {
1462 let apk = tempfile::tempfile().unwrap();
1463 let idsig = tempfile::tempfile().unwrap();
1464
1465 let ret = create_or_update_idsig_file(
1466 &ParcelFileDescriptor::new(apk),
1467 &ParcelFileDescriptor::new(idsig),
1468 );
1469 assert!(ret.is_err(), "should fail");
1470 Ok(())
1471 }
1472
1473 #[test]
1474 fn test_create_or_update_idsig_dir_instead_of_file_for_apk() -> Result<()> {
1475 let tmp_dir = tempfile::TempDir::new().unwrap();
1476 let apk = File::open(tmp_dir.path()).unwrap();
1477 let idsig = tempfile::tempfile().unwrap();
1478
1479 let ret = create_or_update_idsig_file(
1480 &ParcelFileDescriptor::new(apk),
1481 &ParcelFileDescriptor::new(idsig),
1482 );
1483 assert!(ret.is_err(), "should fail");
1484 Ok(())
1485 }
1486
1487 /// Verifies that create_or_update_idsig_file won't oom if a fd that corresponds to a directory
1488 /// on ext4 filesystem is passed.
1489 /// On ext4 lseek on a directory fd will return (off_t)-1 (see:
1490 /// https://bugzilla.kernel.org/show_bug.cgi?id=200043), which will result in
1491 /// create_or_update_idsig_file ooming while attempting to allocate petabytes of memory.
1492 #[test]
1493 fn test_create_or_update_idsig_does_not_crash_dir_on_ext4() -> Result<()> {
1494 // APEXes are backed by the ext4.
1495 let apk = File::open("/apex/com.android.virt/").unwrap();
1496 let idsig = tempfile::tempfile().unwrap();
1497
1498 let ret = create_or_update_idsig_file(
1499 &ParcelFileDescriptor::new(apk),
1500 &ParcelFileDescriptor::new(idsig),
1501 );
1502 assert!(ret.is_err(), "should fail");
1503 Ok(())
1504 }
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001505}