blob: 1c82155e24e66c02bf092dd8560e8b91d5f92f0b [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,
Seungjae Yoofd9a0622022-10-14 10:01:29 +090038 VirtualMachineAppConfig::{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};
David Brazdil73988ea2022-11-11 15:10:32 +000066use rpcbinder::RpcServer;
Jiyong Parkd50a0242021-09-16 21:00:14 +090067use rustutils::system_properties;
Jiyong Parkdcf17412022-02-08 15:07:23 +090068use semver::VersionReq;
David Brazdil73988ea2022-11-11 15:10:32 +000069use std::collections::HashMap;
Andrew Walbrandff3b942021-06-09 15:20:36 +000070use std::convert::TryInto;
Shikha Panward8e35422021-10-11 13:51:27 +000071use std::ffi::CStr;
David Brazdil4b4c5102022-12-19 22:56:20 +000072use std::fs::{create_dir, read_dir, remove_dir, remove_file, set_permissions, File, OpenOptions, Permissions};
Seungjae Yoofd9a0622022-10-14 10:01:29 +090073use std::io::{Error, ErrorKind, Read, Write};
Andrew Walbranb15cd6e2021-07-05 16:38:07 +000074use std::num::NonZeroU32;
David Brazdil4b4c5102022-12-19 22:56:20 +000075use std::os::unix::fs::PermissionsExt;
Andrew Walbrand3a84182021-09-07 14:48:52 +000076use std::os::unix::io::{FromRawFd, IntoRawFd};
David Brazdild4f51a52023-01-11 14:09:27 +000077use std::os::unix::raw::{pid_t, uid_t};
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000078use std::path::{Path, PathBuf};
Andrew Walbran320b5602021-03-04 16:11:12 +000079use std::sync::{Arc, Mutex, Weak};
Seungjae Yoofd9a0622022-10-14 10:01:29 +090080use tombstoned_client::{DebuggerdDumpType, TombstonedConnection};
Andrew Walbrancc0db522021-07-12 17:03:42 +000081use vmconfig::VmConfig;
Andrew Walbranadd38cb2022-10-06 17:01:03 +000082use vsock::{VsockListener, VsockStream};
Jooyung Han35edb8f2021-07-01 16:17:16 +090083use zip::ZipArchive;
David Brazdil4b4c5102022-12-19 22:56:20 +000084use nix::unistd::{chown, Uid};
Andrew Walbrand6dce6f2021-03-05 16:39:08 +000085
David Brazdil41d1a872022-10-05 14:44:19 +010086/// The unique ID of a VM used (together with a port number) for vsock communication.
87pub type Cid = u32;
88
David Brazdil4b4c5102022-12-19 22:56:20 +000089pub const BINDER_SERVICE_IDENTIFIER: &str = "android.system.virtualizationservice";
90
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000091/// Directory in which to write disk image files used while running VMs.
Andrew Walbran488bd072021-07-14 13:29:51 +000092pub const TEMPORARY_DIRECTORY: &str = "/data/misc/virtualizationservice";
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +000093
David Brazdil41d1a872022-10-05 14:44:19 +010094/// The first CID to assign to a guest VM managed by the VirtualizationService. CIDs lower than this
95/// are reserved for the host or other usage.
David Brazdil73988ea2022-11-11 15:10:32 +000096const GUEST_CID_MIN: Cid = 2048;
97const GUEST_CID_MAX: Cid = 65535;
David Brazdil41d1a872022-10-05 14:44:19 +010098
99const SYSPROP_LAST_CID: &str = "virtualizationservice.state.last_cid";
Jiyong Park8611a6c2021-07-09 18:17:44 +0900100
Jooyung Han95884632021-07-06 22:27:54 +0900101/// The size of zero.img.
102/// Gaps in composite disk images are filled with a shared zero.img.
103const ZERO_FILLER_SIZE: u64 = 4096;
104
Jiyong Park9dd389e2021-08-23 20:42:59 +0900105/// Magic string for the instance image
106const ANDROID_VM_INSTANCE_MAGIC: &str = "Android-VM-instance";
107
108/// Version of the instance image format
109const ANDROID_VM_INSTANCE_VERSION: u16 = 1;
110
Shikha Panwar7afc1392022-03-24 08:54:43 +0000111const CHUNK_RECV_MAX_LEN: usize = 1024;
112
Alan Stokes0d1ef782022-09-27 13:46:35 +0100113const MICRODROID_OS_NAME: &str = "microdroid";
114
Shikha Panwar9fd198f2022-11-18 17:43:43 +0000115const UNFORMATTED_STORAGE_MAGIC: &str = "UNFORMATTED-STORAGE";
116
David Brazdil49f96f52022-12-16 21:29:13 +0000117lazy_static! {
David Brazdil4b4c5102022-12-19 22:56:20 +0000118 pub static ref GLOBAL_SERVICE: Strong<dyn IVirtualizationServiceInternal> =
119 wait_for_interface(BINDER_SERVICE_IDENTIFIER)
120 .expect("Could not connect to VirtualizationServiceInternal");
David Brazdil49f96f52022-12-16 21:29:13 +0000121}
122
David Brazdil73988ea2022-11-11 15:10:32 +0000123fn is_valid_guest_cid(cid: Cid) -> bool {
124 (GUEST_CID_MIN..=GUEST_CID_MAX).contains(&cid)
125}
126
Nikita Ioffef1ce9872022-12-09 13:31:59 +0000127fn create_or_update_idsig_file(
128 input_fd: &ParcelFileDescriptor,
129 idsig_fd: &ParcelFileDescriptor,
130) -> Result<()> {
131 let mut input = clone_file(input_fd)?;
132 let metadata = input.metadata().context("failed to get input metadata")?;
133 if !metadata.is_file() {
134 bail!("input is not a regular file");
135 }
136 let mut sig = V4Signature::create(&mut input, 4096, &[], HashAlgorithm::SHA256)
137 .context("failed to create idsig")?;
138
139 let mut output = clone_file(idsig_fd)?;
Nikita Ioffec09b0492022-12-14 20:18:33 +0000140 output.set_len(0).context("failed to set_len on the idsig output")?;
141 sig.write_into(&mut output).context("failed to write idsig")?;
Nikita Ioffef1ce9872022-12-09 13:31:59 +0000142 Ok(())
143}
144
David Brazdil528e0472022-10-10 15:06:02 +0100145/// Singleton service for allocating globally-unique VM resources, such as the CID, and running
146/// singleton servers, like tombstone receiver.
Jooyung Han9900f3d2021-07-06 10:27:54 +0900147#[derive(Debug, Default)]
David Brazdil528e0472022-10-10 15:06:02 +0100148pub struct VirtualizationServiceInternal {
149 state: Arc<Mutex<GlobalState>>,
150}
151
152impl VirtualizationServiceInternal {
David Brazdil4b4c5102022-12-19 22:56:20 +0000153 // TODO(b/245727626): Remove after the source files for virtualizationservice
154 // and virtmgr binaries are split from each other.
155 #[allow(dead_code)]
David Brazdil528e0472022-10-10 15:06:02 +0100156 pub fn init() -> VirtualizationServiceInternal {
157 let service = VirtualizationServiceInternal::default();
158
159 std::thread::spawn(|| {
160 if let Err(e) = handle_stream_connection_tombstoned() {
161 warn!("Error receiving tombstone from guest or writing them. Error: {:?}", e);
162 }
163 });
164
165 service
166 }
167}
168
169impl Interface for VirtualizationServiceInternal {}
170
171impl IVirtualizationServiceInternal for VirtualizationServiceInternal {
David Brazdil4b4c5102022-12-19 22:56:20 +0000172 fn removeMemlockRlimit(&self) -> binder::Result<()> {
173 let pid = get_calling_pid();
174 let lim = libc::rlimit { rlim_cur: libc::RLIM_INFINITY, rlim_max: libc::RLIM_INFINITY };
175
176 // SAFETY - borrowing the new limit struct only
177 let ret = unsafe { libc::prlimit(pid, libc::RLIMIT_MEMLOCK, &lim, std::ptr::null_mut()) };
178
179 match ret {
180 0 => Ok(()),
181 -1 => Err(Status::new_exception_str(
182 ExceptionCode::ILLEGAL_STATE,
183 Some(std::io::Error::last_os_error().to_string()),
184 )),
185 n => Err(Status::new_exception_str(
186 ExceptionCode::ILLEGAL_STATE,
187 Some(format!("Unexpected return value from prlimit(): {n}")),
188 )),
189 }
190 }
191
David Brazdild4f51a52023-01-11 14:09:27 +0000192 fn allocateGlobalVmContext(
193 &self,
194 requester_debug_pid: i32,
195 ) -> binder::Result<Strong<dyn IGlobalVmContext>> {
David Brazdil31c22e62023-01-11 13:52:27 +0000196 let requester_uid = get_calling_uid();
David Brazdild4f51a52023-01-11 14:09:27 +0000197 let requester_debug_pid = requester_debug_pid as pid_t;
David Brazdil528e0472022-10-10 15:06:02 +0100198 let state = &mut *self.state.lock().unwrap();
David Brazdild4f51a52023-01-11 14:09:27 +0000199 state.allocate_vm_context(requester_uid, requester_debug_pid).map_err(|e| {
David Brazdil528e0472022-10-10 15:06:02 +0100200 Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
David Brazdil4b4c5102022-12-19 22:56:20 +0000201 })
David Brazdil528e0472022-10-10 15:06:02 +0100202 }
David Brazdil49f96f52022-12-16 21:29:13 +0000203
204 fn atomVmBooted(&self, atom: &AtomVmBooted) -> Result<(), Status> {
205 forward_vm_booted_atom(atom);
206 Ok(())
207 }
208
209 fn atomVmCreationRequested(&self, atom: &AtomVmCreationRequested) -> Result<(), Status> {
210 forward_vm_creation_atom(atom);
211 Ok(())
212 }
213
214 fn atomVmExited(&self, atom: &AtomVmExited) -> Result<(), Status> {
215 forward_vm_exited_atom(atom);
216 Ok(())
217 }
David Brazdild4f51a52023-01-11 14:09:27 +0000218
219 fn debugListVms(&self) -> binder::Result<Vec<VirtualMachineDebugInfo>> {
220 let state = &mut *self.state.lock().unwrap();
221 let cids = state
222 .held_contexts
223 .iter()
224 .filter_map(|(_, inst)| Weak::upgrade(inst))
225 .map(|vm| VirtualMachineDebugInfo {
226 cid: vm.cid as i32,
227 temporaryDirectory: vm.get_temp_dir().to_string_lossy().to_string(),
228 requesterUid: vm.requester_uid as i32,
229 requesterPid: vm.requester_debug_pid as i32,
230 })
231 .collect();
232 Ok(cids)
233 }
David Brazdil528e0472022-10-10 15:06:02 +0100234}
235
David Brazdil31c22e62023-01-11 13:52:27 +0000236#[derive(Debug, Default)]
237struct GlobalVmInstance {
238 /// The unique CID assigned to the VM for vsock communication.
239 cid: Cid,
David Brazdild4f51a52023-01-11 14:09:27 +0000240 /// UID of the client who requested this VM instance.
241 requester_uid: uid_t,
242 /// PID of the client who requested this VM instance.
243 requester_debug_pid: pid_t,
David Brazdil31c22e62023-01-11 13:52:27 +0000244}
245
246impl GlobalVmInstance {
247 fn get_temp_dir(&self) -> PathBuf {
248 let cid = self.cid;
249 format!("{TEMPORARY_DIRECTORY}/{cid}").into()
250 }
David Brazdil528e0472022-10-10 15:06:02 +0100251}
252
253/// The mutable state of the VirtualizationServiceInternal. There should only be one instance
254/// of this struct.
255#[derive(Debug, Default)]
David Brazdil73988ea2022-11-11 15:10:32 +0000256struct GlobalState {
David Brazdil31c22e62023-01-11 13:52:27 +0000257 /// VM contexts currently allocated to running VMs. A CID is never recycled as long
David Brazdil73988ea2022-11-11 15:10:32 +0000258 /// as there is a strong reference held by a GlobalVmContext.
David Brazdil31c22e62023-01-11 13:52:27 +0000259 held_contexts: HashMap<Cid, Weak<GlobalVmInstance>>,
David Brazdil73988ea2022-11-11 15:10:32 +0000260}
David Brazdil528e0472022-10-10 15:06:02 +0100261
262impl GlobalState {
263 /// Get the next available CID, or an error if we have run out. The last CID used is stored in
264 /// a system property so that restart of virtualizationservice doesn't reuse CID while the host
265 /// Android is up.
David Brazdil31c22e62023-01-11 13:52:27 +0000266 fn get_next_available_cid(&mut self) -> Result<Cid> {
David Brazdil73988ea2022-11-11 15:10:32 +0000267 // Start trying to find a CID from the last used CID + 1. This ensures
David Brazdil8cf8f482022-11-23 14:21:26 +0000268 // that we do not eagerly recycle CIDs. It makes debugging easier but
269 // also means that retrying to allocate a CID, eg. because it is
270 // erroneously occupied by a process, will not recycle the same CID.
David Brazdil73988ea2022-11-11 15:10:32 +0000271 let last_cid_prop =
272 system_properties::read(SYSPROP_LAST_CID)?.and_then(|val| match val.parse::<Cid>() {
273 Ok(num) => {
274 if is_valid_guest_cid(num) {
275 Some(num)
276 } else {
277 error!("Invalid value '{}' of property '{}'", num, SYSPROP_LAST_CID);
278 None
279 }
280 }
David Brazdil528e0472022-10-10 15:06:02 +0100281 Err(_) => {
282 error!("Invalid value '{}' of property '{}'", val, SYSPROP_LAST_CID);
David Brazdil73988ea2022-11-11 15:10:32 +0000283 None
David Brazdil528e0472022-10-10 15:06:02 +0100284 }
David Brazdil73988ea2022-11-11 15:10:32 +0000285 });
286
287 let first_cid = if let Some(last_cid) = last_cid_prop {
David Brazdil31c22e62023-01-11 13:52:27 +0000288 if last_cid == GUEST_CID_MAX {
289 GUEST_CID_MIN
290 } else {
291 last_cid + 1
292 }
David Brazdil73988ea2022-11-11 15:10:32 +0000293 } else {
294 GUEST_CID_MIN
David Brazdil528e0472022-10-10 15:06:02 +0100295 };
David Brazdil73988ea2022-11-11 15:10:32 +0000296
297 let cid = self
298 .find_available_cid(first_cid..=GUEST_CID_MAX)
David Brazdil31c22e62023-01-11 13:52:27 +0000299 .or_else(|| self.find_available_cid(GUEST_CID_MIN..first_cid))
300 .ok_or_else(|| anyhow!("Could not find an available CID."))?;
David Brazdil73988ea2022-11-11 15:10:32 +0000301
David Brazdil31c22e62023-01-11 13:52:27 +0000302 system_properties::write(SYSPROP_LAST_CID, &format!("{}", cid))?;
303 Ok(cid)
David Brazdil73988ea2022-11-11 15:10:32 +0000304 }
305
306 fn find_available_cid<I>(&self, mut range: I) -> Option<Cid>
307 where
308 I: Iterator<Item = Cid>,
309 {
David Brazdil31c22e62023-01-11 13:52:27 +0000310 range.find(|cid| !self.held_contexts.contains_key(cid))
David Brazdil528e0472022-10-10 15:06:02 +0100311 }
David Brazdil4b4c5102022-12-19 22:56:20 +0000312
David Brazdil31c22e62023-01-11 13:52:27 +0000313 fn allocate_vm_context(
314 &mut self,
315 requester_uid: uid_t,
David Brazdild4f51a52023-01-11 14:09:27 +0000316 requester_debug_pid: pid_t,
David Brazdil31c22e62023-01-11 13:52:27 +0000317 ) -> Result<Strong<dyn IGlobalVmContext>> {
318 // Garbage collect unused VM contexts.
319 self.held_contexts.retain(|_, instance| instance.strong_count() > 0);
320
321 let cid = self.get_next_available_cid()?;
David Brazdild4f51a52023-01-11 14:09:27 +0000322 let instance = Arc::new(GlobalVmInstance { cid, requester_uid, requester_debug_pid });
David Brazdil31c22e62023-01-11 13:52:27 +0000323 create_temporary_directory(&instance.get_temp_dir(), requester_uid)?;
324
325 self.held_contexts.insert(cid, Arc::downgrade(&instance));
326 let binder = GlobalVmContext { instance, ..Default::default() };
David Brazdil4b4c5102022-12-19 22:56:20 +0000327 Ok(BnGlobalVmContext::new_binder(binder, BinderFeatures::default()))
328 }
329}
330
David Brazdil31c22e62023-01-11 13:52:27 +0000331fn create_temporary_directory(path: &PathBuf, requester_uid: uid_t) -> Result<()> {
David Brazdil4b4c5102022-12-19 22:56:20 +0000332 if path.as_path().exists() {
David Brazdil31c22e62023-01-11 13:52:27 +0000333 remove_temporary_dir(path).unwrap_or_else(|e| {
David Brazdil4b4c5102022-12-19 22:56:20 +0000334 warn!("Could not delete temporary directory {:?}: {}", path, e);
335 });
336 }
337 // Create a directory that is owned by client's UID but system's GID, and permissions 0700.
338 // If the chown() fails, this will leave behind an empty directory that will get removed
339 // at the next attempt, or if virtualizationservice is restarted.
David Brazdil31c22e62023-01-11 13:52:27 +0000340 create_dir(path).with_context(|| format!("Could not create temporary directory {:?}", path))?;
341 chown(path, Some(Uid::from_raw(requester_uid)), None)
David Brazdil4b4c5102022-12-19 22:56:20 +0000342 .with_context(|| format!("Could not set ownership of temporary directory {:?}", path))?;
David Brazdil31c22e62023-01-11 13:52:27 +0000343 Ok(())
David Brazdil4b4c5102022-12-19 22:56:20 +0000344}
345
346/// Removes a directory owned by a different user by first changing its owner back
347/// to VirtualizationService.
348pub fn remove_temporary_dir(path: &PathBuf) -> Result<()> {
349 if !path.as_path().is_dir() {
350 bail!("Path {:?} is not a directory", path);
351 }
352 chown(path, Some(Uid::current()), None)?;
353 set_permissions(path, Permissions::from_mode(0o700))?;
354 remove_temporary_files(path)?;
355 remove_dir(path)?;
356 Ok(())
357}
358
359pub fn remove_temporary_files(path: &PathBuf) -> Result<()> {
360 for dir_entry in read_dir(path)? {
361 remove_file(dir_entry?.path())?;
362 }
363 Ok(())
David Brazdil528e0472022-10-10 15:06:02 +0100364}
365
366/// Implementation of the AIDL `IGlobalVmContext` interface.
367#[derive(Debug, Default)]
368struct GlobalVmContext {
David Brazdil31c22e62023-01-11 13:52:27 +0000369 /// Strong reference to the context's instance data structure.
370 instance: Arc<GlobalVmInstance>,
David Brazdil73988ea2022-11-11 15:10:32 +0000371 /// Keeps our service process running as long as this VM context exists.
David Brazdil528e0472022-10-10 15:06:02 +0100372 #[allow(dead_code)]
373 lazy_service_guard: LazyServiceGuard,
374}
375
David Brazdil528e0472022-10-10 15:06:02 +0100376impl Interface for GlobalVmContext {}
377
378impl IGlobalVmContext for GlobalVmContext {
379 fn getCid(&self) -> binder::Result<i32> {
David Brazdil31c22e62023-01-11 13:52:27 +0000380 Ok(self.instance.cid as i32)
David Brazdil528e0472022-10-10 15:06:02 +0100381 }
David Brazdil4b4c5102022-12-19 22:56:20 +0000382
383 fn getTemporaryDirectory(&self) -> binder::Result<String> {
David Brazdil31c22e62023-01-11 13:52:27 +0000384 Ok(self.instance.get_temp_dir().to_string_lossy().to_string())
David Brazdil4b4c5102022-12-19 22:56:20 +0000385 }
David Brazdil528e0472022-10-10 15:06:02 +0100386}
387
388/// Implementation of `IVirtualizationService`, the entry point of the AIDL service.
David Brazdil49f96f52022-12-16 21:29:13 +0000389#[derive(Debug, Default)]
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000390pub struct VirtualizationService {
Jiyong Park8611a6c2021-07-09 18:17:44 +0900391 state: Arc<Mutex<State>>,
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000392}
393
Shikha Panward8e35422021-10-11 13:51:27 +0000394impl Interface for VirtualizationService {
395 fn dump(&self, mut file: &File, _args: &[&CStr]) -> Result<(), StatusCode> {
396 check_permission("android.permission.DUMP").or(Err(StatusCode::PERMISSION_DENIED))?;
397 let state = &mut *self.state.lock().unwrap();
398 let vms = state.vms();
399 writeln!(file, "Running {0} VMs:", vms.len()).or(Err(StatusCode::UNKNOWN_ERROR))?;
400 for vm in vms {
401 writeln!(file, "VM CID: {}", vm.cid).or(Err(StatusCode::UNKNOWN_ERROR))?;
402 writeln!(file, "\tState: {:?}", vm.vm_state.lock().unwrap())
403 .or(Err(StatusCode::UNKNOWN_ERROR))?;
404 writeln!(file, "\tPayload state {:?}", vm.payload_state())
405 .or(Err(StatusCode::UNKNOWN_ERROR))?;
406 writeln!(file, "\tProtected: {}", vm.protected).or(Err(StatusCode::UNKNOWN_ERROR))?;
407 writeln!(file, "\ttemporary_directory: {}", vm.temporary_directory.to_string_lossy())
408 .or(Err(StatusCode::UNKNOWN_ERROR))?;
409 writeln!(file, "\trequester_uid: {}", vm.requester_uid)
410 .or(Err(StatusCode::UNKNOWN_ERROR))?;
Shikha Panward8e35422021-10-11 13:51:27 +0000411 writeln!(file, "\trequester_debug_pid: {}", vm.requester_debug_pid)
412 .or(Err(StatusCode::UNKNOWN_ERROR))?;
413 }
414 Ok(())
415 }
416}
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000417
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000418impl IVirtualizationService for VirtualizationService {
Andrew Walbranf8d94112021-09-07 11:45:36 +0000419 /// Creates (but does not start) a new VM with the given configuration, assigning it the next
420 /// available CID.
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000421 ///
422 /// Returns a binder `IVirtualMachine` object referring to it, as a handle for the client.
Andrew Walbranf8d94112021-09-07 11:45:36 +0000423 fn createVm(
Andrew Walbrana89fc132021-03-17 17:08:36 +0000424 &self,
Andrew Walbran3a5a9212021-05-04 17:09:08 +0000425 config: &VirtualMachineConfig,
Jiyong Parkb8182bb2021-10-26 22:53:08 +0900426 console_fd: Option<&ParcelFileDescriptor>,
Andrew Walbrana89fc132021-03-17 17:08:36 +0000427 log_fd: Option<&ParcelFileDescriptor>,
428 ) -> binder::Result<Strong<dyn IVirtualMachine>> {
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000429 let mut is_protected = false;
430 let ret = self.create_vm_internal(config, console_fd, log_fd, &mut is_protected);
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +0000431 write_vm_creation_stats(config, is_protected, &ret);
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000432 ret
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000433 }
Andrew Walbran320b5602021-03-04 16:11:12 +0000434
Andrew Walbrandff3b942021-06-09 15:20:36 +0000435 /// Initialise an empty partition image of the given size to be used as a writable partition.
436 fn initializeWritablePartition(
437 &self,
438 image_fd: &ParcelFileDescriptor,
439 size: i64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900440 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000441 ) -> binder::Result<()> {
Jiyong Park753553b2021-07-12 21:21:09 +0900442 check_manage_access()?;
Andrew Walbrandfc953d2021-06-10 13:59:56 +0000443 let size = size.try_into().map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000444 Status::new_exception_str(
Andrew Walbran806f1542021-06-10 14:07:12 +0000445 ExceptionCode::ILLEGAL_ARGUMENT,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100446 Some(format!("Invalid size {}: {:?}", size, e)),
Andrew Walbran806f1542021-06-10 14:07:12 +0000447 )
Andrew Walbrandff3b942021-06-09 15:20:36 +0000448 })?;
Andrew Walbrandfc953d2021-06-10 13:59:56 +0000449 let image = clone_file(image_fd)?;
Jooyung Han1edd5b92021-10-28 10:58:05 +0900450 // initialize the file. Any data in the file will be erased.
451 image.set_len(0).map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000452 Status::new_service_specific_error_str(
453 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100454 Some(format!("Failed to reset a file: {:?}", e)),
Jooyung Han1edd5b92021-10-28 10:58:05 +0900455 )
456 })?;
Jiyong Park9dd389e2021-08-23 20:42:59 +0900457 let mut part = QcowFile::new(image, size).map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000458 Status::new_service_specific_error_str(
459 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100460 Some(format!("Failed to create QCOW2 image: {:?}", e)),
Andrew Walbran806f1542021-06-10 14:07:12 +0000461 )
Andrew Walbrandfc953d2021-06-10 13:59:56 +0000462 })?;
Andrew Walbrandff3b942021-06-09 15:20:36 +0000463
Jiyong Park9dd389e2021-08-23 20:42:59 +0900464 match partition_type {
465 PartitionType::RAW => Ok(()),
466 PartitionType::ANDROID_VM_INSTANCE => format_as_android_vm_instance(&mut part),
Shikha Panwar9fd198f2022-11-18 17:43:43 +0000467 PartitionType::ENCRYPTEDSTORE => format_as_encryptedstore(&mut part),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900468 _ => Err(Error::new(
469 ErrorKind::Unsupported,
470 format!("Unsupported partition type {:?}", partition_type),
471 )),
472 }
473 .map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000474 Status::new_service_specific_error_str(
475 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100476 Some(format!("Failed to initialize partition as {:?}: {:?}", partition_type, e)),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900477 )
478 })?;
479
Andrew Walbrandff3b942021-06-09 15:20:36 +0000480 Ok(())
481 }
482
Jiyong Park0a248432021-08-20 23:32:39 +0900483 /// Creates or update the idsig file by digesting the input APK file.
484 fn createOrUpdateIdsigFile(
485 &self,
486 input_fd: &ParcelFileDescriptor,
487 idsig_fd: &ParcelFileDescriptor,
488 ) -> binder::Result<()> {
489 // TODO(b/193504400): do this only when (1) idsig_fd is empty or (2) the APK digest in
490 // idsig_fd is different from APK digest in input_fd
491
Jiyong Parkc3ca24f2022-06-28 10:45:15 +0900492 check_manage_access()?;
493
Nikita Ioffef1ce9872022-12-09 13:31:59 +0000494 create_or_update_idsig_file(input_fd, idsig_fd)
495 .map_err(|e| Status::new_service_specific_error_str(-1, Some(format!("{:?}", e))))?;
Jiyong Park0a248432021-08-20 23:32:39 +0900496 Ok(())
497 }
498
Andrew Walbran320b5602021-03-04 16:11:12 +0000499 /// Get a list of all currently running VMs. This method is only intended for debug purposes,
500 /// and as such is only permitted from the shell user.
501 fn debugListVms(&self) -> binder::Result<Vec<VirtualMachineDebugInfo>> {
Andrew Walbran806f1542021-06-10 14:07:12 +0000502 check_debug_access()?;
David Brazdild4f51a52023-01-11 14:09:27 +0000503 GLOBAL_SERVICE.debugListVms()
Andrew Walbran320b5602021-03-04 16:11:12 +0000504 }
David Brazdil3c2ddef2021-03-18 13:09:57 +0000505
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000506 /// Hold a strong reference to a VM in VirtualizationService. This method is only intended for
507 /// debug purposes, and as such is only permitted from the shell user.
Andrei Homescu1415c132021-03-24 02:39:55 +0000508 fn debugHoldVmRef(&self, vmref: &Strong<dyn IVirtualMachine>) -> binder::Result<()> {
Andrew Walbran806f1542021-06-10 14:07:12 +0000509 check_debug_access()?;
David Brazdil3c2ddef2021-03-18 13:09:57 +0000510
David Brazdil3c2ddef2021-03-18 13:09:57 +0000511 let state = &mut *self.state.lock().unwrap();
Andrei Homescu1415c132021-03-24 02:39:55 +0000512 state.debug_hold_vm(vmref.clone());
David Brazdil3c2ddef2021-03-18 13:09:57 +0000513 Ok(())
514 }
515
Andrew Walbranf6bf6862021-05-21 12:41:13 +0000516 /// Drop reference to a VM that is being held by VirtualizationService. Returns the reference if
517 /// the VM was found and None otherwise. This method is only intended for debug purposes, and as
518 /// such is only permitted from the shell user.
David Brazdil3c2ddef2021-03-18 13:09:57 +0000519 fn debugDropVmRef(&self, cid: i32) -> binder::Result<Option<Strong<dyn IVirtualMachine>>> {
Andrew Walbran806f1542021-06-10 14:07:12 +0000520 check_debug_access()?;
David Brazdil3c2ddef2021-03-18 13:09:57 +0000521
522 let state = &mut *self.state.lock().unwrap();
523 Ok(state.debug_drop_vm(cid))
524 }
Andrew Walbran320b5602021-03-04 16:11:12 +0000525}
526
Shikha Panwar7afc1392022-03-24 08:54:43 +0000527fn handle_stream_connection_tombstoned() -> Result<()> {
David Brazdil73988ea2022-11-11 15:10:32 +0000528 // Should not listen for tombstones on a guest VM's port.
529 assert!(!is_valid_guest_cid(VM_TOMBSTONES_SERVICE_PORT as Cid));
Shikha Panwar7afc1392022-03-24 08:54:43 +0000530 let listener =
David Brazdil73988ea2022-11-11 15:10:32 +0000531 VsockListener::bind_with_cid_port(VMADDR_CID_HOST, VM_TOMBSTONES_SERVICE_PORT as Cid)?;
Shikha Panwar7afc1392022-03-24 08:54:43 +0000532 for incoming_stream in listener.incoming() {
533 let mut incoming_stream = match incoming_stream {
534 Err(e) => {
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100535 warn!("invalid incoming connection: {:?}", e);
Shikha Panwar7afc1392022-03-24 08:54:43 +0000536 continue;
537 }
538 Ok(s) => s,
539 };
540 std::thread::spawn(move || {
541 if let Err(e) = handle_tombstone(&mut incoming_stream) {
542 error!("Failed to write tombstone- {:?}", e);
543 }
544 });
545 }
546 Ok(())
547}
548
549fn handle_tombstone(stream: &mut VsockStream) -> Result<()> {
Andrew Walbranadd38cb2022-10-06 17:01:03 +0000550 if let Ok(addr) = stream.peer_addr() {
Shikha Panwar7afc1392022-03-24 08:54:43 +0000551 info!("Vsock Stream connected to cid={} for tombstones", addr.cid());
552 }
553 let tb_connection =
554 TombstonedConnection::connect(std::process::id() as i32, DebuggerdDumpType::Tombstone)
555 .context("Failed to connect to tombstoned")?;
556 let mut text_output = tb_connection
557 .text_output
558 .as_ref()
559 .ok_or_else(|| anyhow!("Could not get file to write the tombstones on"))?;
560 let mut num_bytes_read = 0;
561 loop {
562 let mut chunk_recv = [0; CHUNK_RECV_MAX_LEN];
563 let n = stream
564 .read(&mut chunk_recv)
565 .context("Failed to read tombstone data from Vsock stream")?;
566 if n == 0 {
567 break;
568 }
569 num_bytes_read += n;
570 text_output.write_all(&chunk_recv[0..n]).context("Failed to write guests tombstones")?;
571 }
572 info!("Received {} bytes from guest & wrote to tombstone file", num_bytes_read);
573 tb_connection.notify_completion()?;
574 Ok(())
575}
576
Jiyong Park8611a6c2021-07-09 18:17:44 +0900577impl VirtualizationService {
David Brazdil4b4c5102022-12-19 22:56:20 +0000578 // TODO(b/245727626): Remove after the source files for virtualizationservice
579 // and virtmgr binaries are split from each other.
580 #[allow(dead_code)]
Jiyong Park8611a6c2021-07-09 18:17:44 +0900581 pub fn init() -> VirtualizationService {
David Brazdil49f96f52022-12-16 21:29:13 +0000582 VirtualizationService::default()
Jiyong Park8611a6c2021-07-09 18:17:44 +0900583 }
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000584
David Brazdild4f51a52023-01-11 14:09:27 +0000585 fn create_vm_context(&self, requester_debug_pid: pid_t) -> Result<(VmContext, Cid, PathBuf)> {
David Brazdil8cf8f482022-11-23 14:21:26 +0000586 const NUM_ATTEMPTS: usize = 5;
587
588 for _ in 0..NUM_ATTEMPTS {
David Brazdild4f51a52023-01-11 14:09:27 +0000589 let vm_context = GLOBAL_SERVICE.allocateGlobalVmContext(requester_debug_pid as i32)?;
590 let cid = vm_context.getCid()? as Cid;
591 let temp_dir: PathBuf = vm_context.getTemporaryDirectory()?.into();
David Brazdil8cf8f482022-11-23 14:21:26 +0000592 let service = VirtualMachineService::new_binder(self.state.clone(), cid).as_binder();
593
594 // Start VM service listening for connections from the new CID on port=CID.
David Brazdil8cf8f482022-11-23 14:21:26 +0000595 let port = cid;
David Brazdil3238da42022-11-18 10:04:51 +0000596 match RpcServer::new_vsock(service, cid, port) {
David Brazdil8cf8f482022-11-23 14:21:26 +0000597 Ok(vm_server) => {
598 vm_server.start();
David Brazdild4f51a52023-01-11 14:09:27 +0000599 return Ok((VmContext::new(vm_context, vm_server), cid, temp_dir));
David Brazdil8cf8f482022-11-23 14:21:26 +0000600 }
601 Err(err) => {
602 warn!("Could not start RpcServer on port {}: {}", port, err);
603 }
604 }
605 }
606 bail!("Too many attempts to create VM context failed.");
607 }
608
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000609 fn create_vm_internal(
610 &self,
611 config: &VirtualMachineConfig,
612 console_fd: Option<&ParcelFileDescriptor>,
613 log_fd: Option<&ParcelFileDescriptor>,
614 is_protected: &mut bool,
615 ) -> binder::Result<Strong<dyn IVirtualMachine>> {
616 check_manage_access()?;
Inseob Kim1119d702022-05-02 18:01:58 +0900617
Alan Stokes7bc146c2022-10-20 17:10:32 +0100618 let is_custom = match config {
619 VirtualMachineConfig::RawConfig(_) => true,
620 VirtualMachineConfig::AppConfig(config) => {
621 // Some features are reserved for platform apps only, even when using
622 // VirtualMachineAppConfig:
623 // - controlling CPUs;
624 // - specifying a config file in the APK.
625 !config.taskProfiles.is_empty() || matches!(config.payload, Payload::ConfigPath(_))
Inseob Kim1119d702022-05-02 18:01:58 +0900626 }
Alan Stokes7bc146c2022-10-20 17:10:32 +0100627 };
628 if is_custom {
629 check_use_custom_virtual_machine()?;
Inseob Kim1119d702022-05-02 18:01:58 +0900630 }
631
David Brazdild4f51a52023-01-11 14:09:27 +0000632 let requester_uid = get_calling_uid();
633 let requester_debug_pid = get_calling_pid();
634
635 let (vm_context, cid, temporary_directory) =
636 self.create_vm_context(requester_debug_pid).map_err(|e| {
637 error!("Failed to create VmContext: {:?}", e);
638 Status::new_service_specific_error_str(
639 -1,
640 Some(format!("Failed to create VmContext: {:?}", e)),
641 )
642 })?;
David Brazdil528e0472022-10-10 15:06:02 +0100643
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000644 let state = &mut *self.state.lock().unwrap();
645 let console_fd = console_fd.map(clone_file).transpose()?;
646 let log_fd = log_fd.map(clone_file).transpose()?;
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000647
648 // Counter to generate unique IDs for temporary image files.
649 let mut next_temporary_image_id = 0;
650 // Files which are referred to from composite images. These must be mapped to the crosvm
651 // child process, and not closed before it is started.
652 let mut indirect_files = vec![];
653
Alan Stokes7bc146c2022-10-20 17:10:32 +0100654 let (is_app_config, config) = match config {
655 VirtualMachineConfig::RawConfig(config) => (false, BorrowedOrOwned::Borrowed(config)),
656 VirtualMachineConfig::AppConfig(config) => {
657 let config = load_app_config(config, &temporary_directory).map_err(|e| {
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000658 *is_protected = config.protectedVm;
Alan Stokes0d1ef782022-09-27 13:46:35 +0100659 let message = format!("Failed to load app config: {:?}", e);
660 error!("{}", message);
661 Status::new_service_specific_error_str(-1, Some(message))
Alan Stokes7bc146c2022-10-20 17:10:32 +0100662 })?;
663 (true, BorrowedOrOwned::Owned(config))
664 }
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000665 };
666 let config = config.as_ref();
667 *is_protected = config.protectedVm;
668
669 // Check if partition images are labeled incorrectly. This is to prevent random images
670 // which are not protected by the Android Verified Boot (e.g. bits downloaded by apps) from
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100671 // being loaded in a pVM. This applies to everything in the raw config, and everything but
672 // the non-executable, generated partitions in the app config.
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000673 config
674 .disks
675 .iter()
676 .flat_map(|disk| disk.partitions.iter())
677 .filter(|partition| {
678 if is_app_config {
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100679 !is_safe_app_partition(&partition.label)
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000680 } else {
681 true // all partitions are checked
682 }
683 })
684 .try_for_each(check_label_for_partition)
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100685 .map_err(|e| Status::new_service_specific_error_str(-1, Some(format!("{:?}", e))))?;
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000686
Alan Stokes185fe112023-01-10 16:20:55 +0000687 let kernel = maybe_clone_file(&config.kernel)?;
688 let initrd = maybe_clone_file(&config.initrd)?;
689
690 // In a protected VM, we require custom kernels to come from a trusted source (b/237054515).
691 if config.protectedVm {
692 check_label_for_kernel_files(&kernel, &initrd).map_err(|e| {
693 Status::new_service_specific_error_str(-1, Some(format!("{:?}", e)))
694 })?;
695 }
696
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000697 let zero_filler_path = temporary_directory.join("zero.img");
698 write_zero_filler(&zero_filler_path).map_err(|e| {
Alan Stokes70ccf162022-07-08 11:05:03 +0100699 error!("Failed to make composite image: {:?}", e);
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000700 Status::new_service_specific_error_str(
701 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100702 Some(format!("Failed to make composite image: {:?}", e)),
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000703 )
704 })?;
705
706 // Assemble disk images if needed.
707 let disks = config
708 .disks
709 .iter()
710 .map(|disk| {
711 assemble_disk_image(
712 disk,
713 &zero_filler_path,
714 &temporary_directory,
715 &mut next_temporary_image_id,
716 &mut indirect_files,
717 )
718 })
719 .collect::<Result<Vec<DiskFile>, _>>()?;
720
Jiyong Parke558ab12022-07-07 20:18:55 +0900721 // Creating this ramdump file unconditionally is not harmful as ramdump will be created
722 // only when the VM is configured as such. `ramdump_write` is sent to crosvm and will
Jiyong Park4afe2012022-07-08 05:38:49 +0900723 // be the backing store for the /dev/hvc1 where VM will emit ramdump to. `ramdump_read`
Jiyong Parke558ab12022-07-07 20:18:55 +0900724 // will be sent back to the client (i.e. the VM owner) for readout.
725 let ramdump_path = temporary_directory.join("ramdump");
726 let ramdump = prepare_ramdump_file(&ramdump_path).map_err(|e| {
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100727 error!("Failed to prepare ramdump file: {:?}", e);
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000728 Status::new_service_specific_error_str(
729 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100730 Some(format!("Failed to prepare ramdump file: {:?}", e)),
Jiyong Parke558ab12022-07-07 20:18:55 +0900731 )
732 })?;
733
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000734 // Actually start the VM.
735 let crosvm_config = CrosvmConfig {
736 cid,
Seungjae Yoo62085c02022-08-12 04:44:52 +0000737 name: config.name.clone(),
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000738 bootloader: maybe_clone_file(&config.bootloader)?,
Alan Stokes185fe112023-01-10 16:20:55 +0000739 kernel,
740 initrd,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000741 disks,
742 params: config.params.to_owned(),
743 protected: *is_protected,
744 memory_mib: config.memoryMib.try_into().ok().and_then(NonZeroU32::new),
745 cpus: config.numCpus.try_into().ok().and_then(NonZeroU32::new),
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900746 task_profiles: config.taskProfiles.clone(),
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000747 console_fd,
748 log_fd,
Jiyong Parke558ab12022-07-07 20:18:55 +0900749 ramdump: Some(ramdump),
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000750 indirect_files,
751 platform_version: parse_platform_version_req(&config.platformVersion)?,
Jiyong Parke6ed0f92022-06-22 00:13:00 +0900752 detect_hangup: is_app_config,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000753 };
754 let instance = Arc::new(
David Brazdil528e0472022-10-10 15:06:02 +0100755 VmInstance::new(
756 crosvm_config,
757 temporary_directory,
758 requester_uid,
759 requester_debug_pid,
760 vm_context,
761 )
762 .map_err(|e| {
763 error!("Failed to create VM with config {:?}: {:?}", config, e);
764 Status::new_service_specific_error_str(
765 -1,
766 Some(format!("Failed to create VM: {:?}", e)),
767 )
768 })?,
Shikha Panwar061aa2c2022-04-05 12:52:56 +0000769 );
770 state.add_vm(Arc::downgrade(&instance));
771 Ok(VirtualMachine::create(instance))
772 }
Jiyong Park8611a6c2021-07-09 18:17:44 +0900773}
774
Andrew Walbranfbb39d22021-07-28 17:01:25 +0000775fn write_zero_filler(zero_filler_path: &Path) -> Result<()> {
Jooyung Han95884632021-07-06 22:27:54 +0900776 let file = OpenOptions::new()
777 .create_new(true)
778 .read(true)
779 .write(true)
780 .open(zero_filler_path)
781 .with_context(|| "Failed to create zero.img")?;
782 file.set_len(ZERO_FILLER_SIZE)?;
Andrew Walbranfbb39d22021-07-28 17:01:25 +0000783 Ok(())
Jooyung Han95884632021-07-06 22:27:54 +0900784}
785
Jiyong Park9dd389e2021-08-23 20:42:59 +0900786fn format_as_android_vm_instance(part: &mut dyn Write) -> std::io::Result<()> {
787 part.write_all(ANDROID_VM_INSTANCE_MAGIC.as_bytes())?;
788 part.write_all(&ANDROID_VM_INSTANCE_VERSION.to_le_bytes())?;
789 part.flush()
790}
791
Shikha Panwar9fd198f2022-11-18 17:43:43 +0000792fn format_as_encryptedstore(part: &mut dyn Write) -> std::io::Result<()> {
793 part.write_all(UNFORMATTED_STORAGE_MAGIC.as_bytes())?;
794 part.flush()
795}
796
Jiyong Parke558ab12022-07-07 20:18:55 +0900797fn prepare_ramdump_file(ramdump_path: &Path) -> Result<File> {
Chris Wailes9b866f02022-11-16 15:17:16 -0800798 File::create(ramdump_path).context(format!("Failed to create ramdump file {:?}", &ramdump_path))
Jiyong Parke558ab12022-07-07 20:18:55 +0900799}
800
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000801/// Given the configuration for a disk image, assembles the `DiskFile` to pass to crosvm.
802///
803/// This may involve assembling a composite disk from a set of partition images.
804fn assemble_disk_image(
805 disk: &DiskImage,
Jooyung Han95884632021-07-06 22:27:54 +0900806 zero_filler_path: &Path,
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000807 temporary_directory: &Path,
808 next_temporary_image_id: &mut u64,
809 indirect_files: &mut Vec<File>,
Andrew Walbran806f1542021-06-10 14:07:12 +0000810) -> Result<DiskFile, Status> {
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000811 let image = if !disk.partitions.is_empty() {
812 if disk.image.is_some() {
813 warn!("DiskImage {:?} contains both image and partitions.", disk);
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000814 return Err(Status::new_exception_str(
Andrew Walbran806f1542021-06-10 14:07:12 +0000815 ExceptionCode::ILLEGAL_ARGUMENT,
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000816 Some("DiskImage contains both image and partitions."),
Andrew Walbran806f1542021-06-10 14:07:12 +0000817 ));
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000818 }
819
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000820 let composite_image_filenames =
821 make_composite_image_filenames(temporary_directory, next_temporary_image_id);
822 let (image, partition_files) = make_composite_image(
823 &disk.partitions,
Jooyung Han95884632021-07-06 22:27:54 +0900824 zero_filler_path,
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000825 &composite_image_filenames.composite,
826 &composite_image_filenames.header,
827 &composite_image_filenames.footer,
828 )
829 .map_err(|e| {
Alan Stokes70ccf162022-07-08 11:05:03 +0100830 error!("Failed to make composite image with config {:?}: {:?}", disk, e);
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000831 Status::new_service_specific_error_str(
832 -1,
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100833 Some(format!("Failed to make composite image: {:?}", e)),
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000834 )
835 })?;
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000836
837 // Pass the file descriptors for the various partition files to crosvm when it
838 // is run.
839 indirect_files.extend(partition_files);
840
841 image
842 } else if let Some(image) = &disk.image {
843 clone_file(image)?
844 } else {
845 warn!("DiskImage {:?} didn't contain image or partitions.", disk);
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000846 return Err(Status::new_exception_str(
Andrew Walbran806f1542021-06-10 14:07:12 +0000847 ExceptionCode::ILLEGAL_ARGUMENT,
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000848 Some("DiskImage didn't contain image or partitions."),
Andrew Walbran806f1542021-06-10 14:07:12 +0000849 ));
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000850 };
851
852 Ok(DiskFile { image, writable: disk.writable })
853}
854
Jooyung Han21e9b922021-06-26 04:14:16 +0900855fn load_app_config(
856 config: &VirtualMachineAppConfig,
857 temporary_directory: &Path,
Jooyung Hanadfb76c2021-06-28 17:29:30 +0900858) -> Result<VirtualMachineRawConfig> {
Andrew Walbrancc0db522021-07-12 17:03:42 +0000859 let apk_file = clone_file(config.apk.as_ref().unwrap())?;
860 let idsig_file = clone_file(config.idsig.as_ref().unwrap())?;
Jiyong Park8d081812021-07-23 17:45:04 +0900861 let instance_file = clone_file(config.instanceImage.as_ref().unwrap())?;
Jooyung Han21e9b922021-06-26 04:14:16 +0900862
Shikha Panwar22e70452022-10-10 18:32:55 +0000863 let storage_image = if let Some(file) = config.encryptedStorageImage.as_ref() {
864 Some(clone_file(file)?)
865 } else {
866 None
867 };
868
Alan Stokes0d1ef782022-09-27 13:46:35 +0100869 let vm_payload_config = match &config.payload {
870 Payload::ConfigPath(config_path) => {
871 load_vm_payload_config_from_file(&apk_file, config_path.as_str())
872 .with_context(|| format!("Couldn't read config from {}", config_path))?
873 }
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000874 Payload::PayloadConfig(payload_config) => create_vm_payload_config(payload_config)?,
Alan Stokes0d1ef782022-09-27 13:46:35 +0100875 };
Jooyung Han21e9b922021-06-26 04:14:16 +0900876
Alan Stokes0d1ef782022-09-27 13:46:35 +0100877 // For now, the only supported OS is Microdroid
878 let os_name = vm_payload_config.os.name.as_str();
879 if os_name != MICRODROID_OS_NAME {
Andrew Walbrancc0db522021-07-12 17:03:42 +0000880 bail!("Unknown OS \"{}\"", os_name);
Jooyung Han35edb8f2021-07-01 16:17:16 +0900881 }
Andrew Walbrancc0db522021-07-12 17:03:42 +0000882
883 // It is safe to construct a filename based on the os_name because we've already checked that it
884 // is one of the allowed values.
Jooyung Han21e9b922021-06-26 04:14:16 +0900885 let vm_config_path = PathBuf::from(format!("/apex/com.android.virt/etc/{}.json", os_name));
886 let vm_config_file = File::open(vm_config_path)?;
Andrew Walbrancc0db522021-07-12 17:03:42 +0000887 let mut vm_config = VmConfig::load(&vm_config_file)?.to_parcelable()?;
Jooyung Han21e9b922021-06-26 04:14:16 +0900888
Andrew Walbrancc045902021-07-27 16:06:17 +0000889 if config.memoryMib > 0 {
890 vm_config.memoryMib = config.memoryMib;
Andrew Walbran45bcb0c2021-07-14 15:02:06 +0000891 }
892
Seungjae Yoo62085c02022-08-12 04:44:52 +0000893 vm_config.name = config.name.clone();
Andrew Walbran3994f002022-01-27 17:33:45 +0000894 vm_config.protectedVm = config.protectedVm;
Jiyong Park032615f2022-01-10 13:55:34 +0900895 vm_config.numCpus = config.numCpus;
Jiyong Parkdfe16d62022-04-20 17:32:12 +0900896 vm_config.taskProfiles = config.taskProfiles.clone();
Jiyong Park032615f2022-01-10 13:55:34 +0900897
Shikha Panwar22e70452022-10-10 18:32:55 +0000898 // Microdroid takes additional init ramdisk & (optionally) storage image
899 add_microdroid_system_images(config, instance_file, storage_image, &mut vm_config)?;
900
901 // Include Microdroid payload disk (contains apks, idsigs) in vm config
902 add_microdroid_payload_images(
Alan Stokes0d1ef782022-09-27 13:46:35 +0100903 config,
904 temporary_directory,
905 apk_file,
906 idsig_file,
Alan Stokes0d1ef782022-09-27 13:46:35 +0100907 &vm_payload_config,
908 &mut vm_config,
909 )?;
Jooyung Han21e9b922021-06-26 04:14:16 +0900910
Andrew Walbrancc0db522021-07-12 17:03:42 +0000911 Ok(vm_config)
Jooyung Han21e9b922021-06-26 04:14:16 +0900912}
913
Alan Stokes0d1ef782022-09-27 13:46:35 +0100914fn load_vm_payload_config_from_file(apk_file: &File, config_path: &str) -> Result<VmPayloadConfig> {
915 let mut apk_zip = ZipArchive::new(apk_file)?;
916 let config_file = apk_zip.by_name(config_path)?;
917 Ok(serde_json::from_reader(config_file)?)
918}
919
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000920fn create_vm_payload_config(
921 payload_config: &VirtualMachinePayloadConfig,
922) -> Result<VmPayloadConfig> {
Alan Stokes0d1ef782022-09-27 13:46:35 +0100923 // There isn't an actual config file. Construct a synthetic VmPayloadConfig from the explicit
924 // parameters we've been given. Microdroid will do something equivalent inside the VM using the
925 // payload config that we send it via the metadata file.
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000926
927 let payload_binary_name = &payload_config.payloadBinaryName;
928 if payload_binary_name.contains('/') {
929 bail!("Payload binary name must not specify a path: {payload_binary_name}");
930 }
931
932 let task = Task { type_: TaskType::MicrodroidLauncher, command: payload_binary_name.clone() };
933 Ok(VmPayloadConfig {
Alan Stokes0d1ef782022-09-27 13:46:35 +0100934 os: OsConfig { name: MICRODROID_OS_NAME.to_owned() },
935 task: Some(task),
936 apexes: vec![],
937 extra_apks: vec![],
938 prefer_staged: false,
Alan Stokes1f417c92022-09-29 15:13:28 +0100939 export_tombstones: false,
Alan Stokes0d1ef782022-09-27 13:46:35 +0100940 enable_authfs: false,
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000941 })
Alan Stokes0d1ef782022-09-27 13:46:35 +0100942}
943
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000944/// Generates a unique filename to use for a composite disk image.
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000945fn make_composite_image_filenames(
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000946 temporary_directory: &Path,
947 next_temporary_image_id: &mut u64,
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000948) -> CompositeImageFilenames {
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000949 let id = *next_temporary_image_id;
950 *next_temporary_image_id += 1;
Andrew Walbran3eca16c2021-06-14 11:15:14 +0000951 CompositeImageFilenames {
952 composite: temporary_directory.join(format!("composite-{}.img", id)),
953 header: temporary_directory.join(format!("composite-{}-header.img", id)),
954 footer: temporary_directory.join(format!("composite-{}-footer.img", id)),
955 }
956}
957
958/// Filenames for a composite disk image, including header and footer partitions.
959#[derive(Clone, Debug, Eq, PartialEq)]
960struct CompositeImageFilenames {
961 /// The composite disk image itself.
962 composite: PathBuf,
963 /// The header partition image.
964 header: PathBuf,
965 /// The footer partition image.
966 footer: PathBuf,
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +0000967}
968
Jiyong Park753553b2021-07-12 21:21:09 +0900969/// Checks whether the caller has a specific permission
970fn check_permission(perm: &str) -> binder::Result<()> {
David Brazdil1f530702022-10-03 12:18:10 +0100971 let calling_pid = get_calling_pid();
972 let calling_uid = get_calling_uid();
Jiyong Park753553b2021-07-12 21:21:09 +0900973 // Root can do anything
974 if calling_uid == 0 {
975 return Ok(());
976 }
977 let perm_svc: Strong<dyn IPermissionController::IPermissionController> =
978 binder::get_interface("permission")?;
979 if perm_svc.checkPermission(perm, calling_pid, calling_uid as i32)? {
Andrew Walbran806f1542021-06-10 14:07:12 +0000980 Ok(())
981 } else {
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000982 Err(Status::new_exception_str(
Jiyong Park753553b2021-07-12 21:21:09 +0900983 ExceptionCode::SECURITY,
Andrew Walbrandcf9d582022-08-03 11:25:24 +0000984 Some(format!("does not have the {} permission", perm)),
Jiyong Park753553b2021-07-12 21:21:09 +0900985 ))
Andrew Walbran806f1542021-06-10 14:07:12 +0000986 }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +0000987}
988
Jiyong Park753553b2021-07-12 21:21:09 +0900989/// Check whether the caller of the current Binder method is allowed to call debug methods.
990fn check_debug_access() -> binder::Result<()> {
991 check_permission("android.permission.DEBUG_VIRTUAL_MACHINE")
992}
993
994/// Check whether the caller of the current Binder method is allowed to manage VMs
995fn check_manage_access() -> binder::Result<()> {
996 check_permission("android.permission.MANAGE_VIRTUAL_MACHINE")
997}
998
Inseob Kim1119d702022-05-02 18:01:58 +0900999/// Check whether the caller of the current Binder method is allowed to create custom VMs
1000fn check_use_custom_virtual_machine() -> binder::Result<()> {
1001 check_permission("android.permission.USE_CUSTOM_VIRTUAL_MACHINE")
1002}
1003
Alan Stokes185fe112023-01-10 16:20:55 +00001004/// Return whether a partition is exempt from selinux label checks, because we know that it does
1005/// not contain code and is likely to be generated in an app-writable directory.
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001006fn is_safe_app_partition(label: &str) -> bool {
Shikha Panwara2ff8c52022-11-30 19:25:46 +00001007 // See add_microdroid_system_images & add_microdroid_payload_images in payload.rs.
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001008 label == "vm-instance"
Shikha Panwara2ff8c52022-11-30 19:25:46 +00001009 || label == "encryptedstore"
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001010 || label == "microdroid-apk-idsig"
1011 || label == "payload-metadata"
1012 || label.starts_with("extra-idsig-")
1013}
1014
Alan Stokes185fe112023-01-10 16:20:55 +00001015/// Check that a file SELinux label is acceptable.
1016///
1017/// We only want to allow code in a VM to be sourced from places that apps, and the
1018/// system, do not have write access to.
1019///
1020/// Note that sepolicy must also grant read access for these types to both virtualization
1021/// service and crosvm.
1022///
1023/// App private data files are deliberately excluded, to avoid arbitrary payloads being run on
1024/// user devices (W^X).
1025fn check_label_is_allowed(context: &SeContext) -> Result<()> {
1026 match context.selinux_type()? {
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001027 | "system_file" // immutable dm-verity protected partition
1028 | "apk_data_file" // APKs of an installed app
Shikha Panwara2ff8c52022-11-30 19:25:46 +00001029 | "staging_data_file" // updated/staged APEX images
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001030 | "shell_data_file" // test files created via adb shell
1031 => Ok(()),
Alan Stokes185fe112023-01-10 16:20:55 +00001032 _ => bail!("Label {} is not allowed", context),
Jiyong Park029977d2021-11-24 21:56:49 +09001033 }
1034}
1035
Alan Stokes185fe112023-01-10 16:20:55 +00001036fn check_label_for_partition(partition: &Partition) -> Result<()> {
1037 let file = partition.image.as_ref().unwrap().as_ref();
1038 check_label_is_allowed(&getfilecon(file)?)
1039 .with_context(|| format!("Partition {} invalid", &partition.label))
1040}
1041
1042fn check_label_for_kernel_files(kernel: &Option<File>, initrd: &Option<File>) -> Result<()> {
1043 if let Some(f) = kernel {
1044 check_label_for_file(f, "kernel")?;
1045 }
1046 if let Some(f) = initrd {
1047 check_label_for_file(f, "initrd")?;
1048 }
1049 Ok(())
1050}
1051fn check_label_for_file(file: &File, name: &str) -> Result<()> {
1052 check_label_is_allowed(&getfilecon(file)?).with_context(|| format!("{} file invalid", name))
1053}
1054
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001055/// Implementation of the AIDL `IVirtualMachine` interface. Used as a handle to a VM.
1056#[derive(Debug)]
1057struct VirtualMachine {
1058 instance: Arc<VmInstance>,
1059}
1060
1061impl VirtualMachine {
1062 fn create(instance: Arc<VmInstance>) -> Strong<dyn IVirtualMachine> {
David Brazdil4b4c5102022-12-19 22:56:20 +00001063 BnVirtualMachine::new_binder(VirtualMachine { instance }, BinderFeatures::default())
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001064 }
1065}
1066
1067impl Interface for VirtualMachine {}
1068
1069impl IVirtualMachine for VirtualMachine {
1070 fn getCid(&self) -> binder::Result<i32> {
Jiyong Park753553b2021-07-12 21:21:09 +09001071 // Don't check permission. The owner of the VM might have passed this binder object to
1072 // others.
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001073 Ok(self.instance.cid as i32)
1074 }
Andrew Walbrandae07162021-03-12 17:05:20 +00001075
Andrew Walbran6b650662021-09-07 13:13:23 +00001076 fn getState(&self) -> binder::Result<VirtualMachineState> {
Jiyong Park753553b2021-07-12 21:21:09 +09001077 // Don't check permission. The owner of the VM might have passed this binder object to
1078 // others.
Andrew Walbran6b650662021-09-07 13:13:23 +00001079 Ok(get_state(&self.instance))
Andrew Walbrandae07162021-03-12 17:05:20 +00001080 }
1081
1082 fn registerCallback(
1083 &self,
1084 callback: &Strong<dyn IVirtualMachineCallback>,
1085 ) -> binder::Result<()> {
Jiyong Park753553b2021-07-12 21:21:09 +09001086 // Don't check permission. The owner of the VM might have passed this binder object to
1087 // others.
1088 //
Andrew Walbrandae07162021-03-12 17:05:20 +00001089 // TODO: Should this give an error if the VM is already dead?
1090 self.instance.callbacks.add(callback.clone());
1091 Ok(())
1092 }
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001093
Andrew Walbranf8d94112021-09-07 11:45:36 +00001094 fn start(&self) -> binder::Result<()> {
1095 self.instance.start().map_err(|e| {
1096 error!("Error starting VM with CID {}: {:?}", self.instance.cid, e);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001097 Status::new_service_specific_error_str(-1, Some(e.to_string()))
Andrew Walbranf8d94112021-09-07 11:45:36 +00001098 })
1099 }
1100
Inseob Kima446f802022-07-11 19:46:37 +09001101 fn stop(&self) -> binder::Result<()> {
1102 self.instance.kill().map_err(|e| {
1103 error!("Error stopping VM with CID {}: {:?}", self.instance.cid, e);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001104 Status::new_service_specific_error_str(-1, Some(e.to_string()))
Inseob Kima446f802022-07-11 19:46:37 +09001105 })
1106 }
1107
Keir Frasercdd4b112022-11-24 14:02:25 +00001108 fn onTrimMemory(&self, level: MemoryTrimLevel) -> binder::Result<()> {
1109 self.instance.trim_memory(level).map_err(|e| {
1110 error!("Error trimming VM with CID {}: {:?}", self.instance.cid, e);
1111 Status::new_service_specific_error_str(-1, Some(e.to_string()))
1112 })
1113 }
1114
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001115 fn connectVsock(&self, port: i32) -> binder::Result<ParcelFileDescriptor> {
Andrew Walbranf8d94112021-09-07 11:45:36 +00001116 if !matches!(&*self.instance.vm_state.lock().unwrap(), VmState::Running { .. }) {
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001117 return Err(Status::new_service_specific_error_str(-1, Some("VM is not running")));
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001118 }
Alan Stokes10c47672022-12-13 17:17:08 +00001119 let port = port as u32;
1120 if port < 1024 {
1121 return Err(Status::new_service_specific_error_str(
1122 -1,
1123 Some(format!("Can't connect to privileged port {port}")),
1124 ));
1125 }
1126 let stream = VsockStream::connect_with_cid_port(self.instance.cid, port).map_err(|e| {
1127 Status::new_service_specific_error_str(-1, Some(format!("Failed to connect: {:?}", e)))
1128 })?;
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001129 Ok(vsock_stream_to_pfd(stream))
1130 }
Andrew Walbrandae07162021-03-12 17:05:20 +00001131}
1132
1133impl Drop for VirtualMachine {
1134 fn drop(&mut self) {
1135 debug!("Dropping {:?}", self);
Inseob Kima446f802022-07-11 19:46:37 +09001136 if let Err(e) = self.instance.kill() {
1137 debug!("Error stopping dropped VM with CID {}: {:?}", self.instance.cid, e);
1138 }
Andrew Walbrandae07162021-03-12 17:05:20 +00001139 }
1140}
1141
1142/// A set of Binders to be called back in response to various events on the VM, such as when it
1143/// dies.
1144#[derive(Debug, Default)]
1145pub struct VirtualMachineCallbacks(Mutex<Vec<Strong<dyn IVirtualMachineCallback>>>);
1146
1147impl VirtualMachineCallbacks {
Jiyong Park8611a6c2021-07-09 18:17:44 +09001148 /// Call all registered callbacks to notify that the payload has started.
David Brazdil451cc962022-10-14 14:08:12 +01001149 pub fn notify_payload_started(&self, cid: Cid) {
Jiyong Park8611a6c2021-07-09 18:17:44 +09001150 let callbacks = &*self.0.lock().unwrap();
Jiyong Park8611a6c2021-07-09 18:17:44 +09001151 for callback in callbacks {
David Brazdil451cc962022-10-14 14:08:12 +01001152 if let Err(e) = callback.onPayloadStarted(cid as i32) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001153 error!("Error notifying payload start event from VM CID {}: {:?}", cid, e);
Jiyong Park8611a6c2021-07-09 18:17:44 +09001154 }
1155 }
1156 }
1157
Inseob Kim14cb8692021-08-31 21:50:39 +09001158 /// Call all registered callbacks to notify that the payload is ready to serve.
1159 pub fn notify_payload_ready(&self, cid: Cid) {
1160 let callbacks = &*self.0.lock().unwrap();
1161 for callback in callbacks {
1162 if let Err(e) = callback.onPayloadReady(cid as i32) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001163 error!("Error notifying payload ready event from VM CID {}: {:?}", cid, e);
Inseob Kim14cb8692021-08-31 21:50:39 +09001164 }
1165 }
1166 }
1167
Inseob Kim2444af92021-08-31 01:22:50 +09001168 /// Call all registered callbacks to notify that the payload has finished.
1169 pub fn notify_payload_finished(&self, cid: Cid, exit_code: i32) {
1170 let callbacks = &*self.0.lock().unwrap();
1171 for callback in callbacks {
1172 if let Err(e) = callback.onPayloadFinished(cid as i32, exit_code) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001173 error!("Error notifying payload finish event from VM CID {}: {:?}", cid, e);
Inseob Kim2444af92021-08-31 01:22:50 +09001174 }
1175 }
1176 }
1177
Jooyung Handd0a1732021-11-23 15:26:20 +09001178 /// Call all registered callbacks to say that the VM encountered an error.
Alan Stokes2bead0d2022-09-05 16:58:34 +01001179 pub fn notify_error(&self, cid: Cid, error_code: ErrorCode, message: &str) {
Jooyung Handd0a1732021-11-23 15:26:20 +09001180 let callbacks = &*self.0.lock().unwrap();
1181 for callback in callbacks {
1182 if let Err(e) = callback.onError(cid as i32, error_code, message) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001183 error!("Error notifying error event from VM CID {}: {:?}", cid, e);
Jooyung Handd0a1732021-11-23 15:26:20 +09001184 }
1185 }
1186 }
1187
Andrew Walbrandae07162021-03-12 17:05:20 +00001188 /// Call all registered callbacks to say that the VM has died.
Andrew Walbranc92d35f2022-01-12 12:45:19 +00001189 pub fn callback_on_died(&self, cid: Cid, reason: DeathReason) {
Andrew Walbrandae07162021-03-12 17:05:20 +00001190 let callbacks = &*self.0.lock().unwrap();
1191 for callback in callbacks {
Andrew Walbranc92d35f2022-01-12 12:45:19 +00001192 if let Err(e) = callback.onDied(cid as i32, reason) {
Alan Stokes70ccf162022-07-08 11:05:03 +01001193 error!("Error notifying exit of VM CID {}: {:?}", cid, e);
Andrew Walbrandae07162021-03-12 17:05:20 +00001194 }
1195 }
1196 }
1197
1198 /// Add a new callback to the set.
1199 fn add(&self, callback: Strong<dyn IVirtualMachineCallback>) {
1200 self.0.lock().unwrap().push(callback);
1201 }
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001202}
1203
Andrew Walbranf6bf6862021-05-21 12:41:13 +00001204/// The mutable state of the VirtualizationService. There should only be one instance of this
1205/// struct.
Chris Wailes641fc4a2021-12-01 15:03:21 -08001206#[derive(Debug, Default)]
Andrew Walbrand6dce6f2021-03-05 16:39:08 +00001207struct State {
Andrew Walbran320b5602021-03-04 16:11:12 +00001208 /// The VMs which have been started. When VMs are started a weak reference is added to this list
1209 /// while a strong reference is returned to the caller over Binder. Once all copies of the
1210 /// Binder client are dropped the weak reference here will become invalid, and will be removed
1211 /// from the list opportunistically the next time `add_vm` is called.
1212 vms: Vec<Weak<VmInstance>>,
David Brazdil3c2ddef2021-03-18 13:09:57 +00001213
1214 /// Vector of strong VM references held on behalf of users that cannot hold them themselves.
1215 /// This is only used for debugging purposes.
1216 debug_held_vms: Vec<Strong<dyn IVirtualMachine>>,
Andrew Walbran320b5602021-03-04 16:11:12 +00001217}
1218
1219impl State {
Andrew Walbrandae07162021-03-12 17:05:20 +00001220 /// Get a list of VMs which still have Binder references to them.
Andrew Walbran320b5602021-03-04 16:11:12 +00001221 fn vms(&self) -> Vec<Arc<VmInstance>> {
1222 // Attempt to upgrade the weak pointers to strong pointers.
1223 self.vms.iter().filter_map(Weak::upgrade).collect()
1224 }
1225
1226 /// Add a new VM to the list.
1227 fn add_vm(&mut self, vm: Weak<VmInstance>) {
1228 // Garbage collect any entries from the stored list which no longer exist.
1229 self.vms.retain(|vm| vm.strong_count() > 0);
1230
1231 // Actually add the new VM.
1232 self.vms.push(vm);
1233 }
David Brazdil3c2ddef2021-03-18 13:09:57 +00001234
Jiyong Park8611a6c2021-07-09 18:17:44 +09001235 /// Get a VM that corresponds to the given cid
1236 fn get_vm(&self, cid: Cid) -> Option<Arc<VmInstance>> {
1237 self.vms().into_iter().find(|vm| vm.cid == cid)
1238 }
1239
David Brazdil3c2ddef2021-03-18 13:09:57 +00001240 /// Store a strong VM reference.
1241 fn debug_hold_vm(&mut self, vm: Strong<dyn IVirtualMachine>) {
1242 self.debug_held_vms.push(vm);
1243 }
1244
1245 /// Retrieve and remove a strong VM reference.
1246 fn debug_drop_vm(&mut self, cid: i32) -> Option<Strong<dyn IVirtualMachine>> {
1247 let pos = self.debug_held_vms.iter().position(|vm| vm.getCid() == Ok(cid))?;
Alan Stokes7e54e292021-09-09 11:37:56 +01001248 let vm = self.debug_held_vms.swap_remove(pos);
Alan Stokes7e54e292021-09-09 11:37:56 +01001249 Some(vm)
David Brazdil3c2ddef2021-03-18 13:09:57 +00001250 }
Jiyong Parkd50a0242021-09-16 21:00:14 +09001251}
1252
Andrew Walbran6b650662021-09-07 13:13:23 +00001253/// Gets the `VirtualMachineState` of the given `VmInstance`.
1254fn get_state(instance: &VmInstance) -> VirtualMachineState {
Andrew Walbranf8d94112021-09-07 11:45:36 +00001255 match &*instance.vm_state.lock().unwrap() {
1256 VmState::NotStarted { .. } => VirtualMachineState::NOT_STARTED,
1257 VmState::Running { .. } => match instance.payload_state() {
Andrew Walbran6b650662021-09-07 13:13:23 +00001258 PayloadState::Starting => VirtualMachineState::STARTING,
1259 PayloadState::Started => VirtualMachineState::STARTED,
1260 PayloadState::Ready => VirtualMachineState::READY,
1261 PayloadState::Finished => VirtualMachineState::FINISHED,
Jiyong Parka4eebde2022-07-12 18:01:12 +09001262 PayloadState::Hangup => VirtualMachineState::DEAD,
Andrew Walbranf8d94112021-09-07 11:45:36 +00001263 },
1264 VmState::Dead => VirtualMachineState::DEAD,
1265 VmState::Failed => VirtualMachineState::DEAD,
Andrew Walbran6b650662021-09-07 13:13:23 +00001266 }
1267}
1268
Andrew Walbranf5fbb7d2021-05-12 17:15:48 +00001269/// Converts a `&ParcelFileDescriptor` to a `File` by cloning the file.
Seungjae Yoo0a8c84c2022-07-11 08:19:15 +00001270pub fn clone_file(file: &ParcelFileDescriptor) -> Result<File, Status> {
Andrew Walbran806f1542021-06-10 14:07:12 +00001271 file.as_ref().try_clone().map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001272 Status::new_exception_str(
Andrew Walbran806f1542021-06-10 14:07:12 +00001273 ExceptionCode::BAD_PARCELABLE,
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001274 Some(format!("Failed to clone File from ParcelFileDescriptor: {:?}", e)),
Andrew Walbran806f1542021-06-10 14:07:12 +00001275 )
1276 })
1277}
1278
Andrew Walbrand3a84182021-09-07 14:48:52 +00001279/// Converts an `&Option<ParcelFileDescriptor>` to an `Option<File>` by cloning the file.
1280fn maybe_clone_file(file: &Option<ParcelFileDescriptor>) -> Result<Option<File>, Status> {
1281 file.as_ref().map(clone_file).transpose()
1282}
1283
Andrew Walbrancbe8b082021-08-06 15:42:11 +00001284/// Converts a `VsockStream` to a `ParcelFileDescriptor`.
1285fn vsock_stream_to_pfd(stream: VsockStream) -> ParcelFileDescriptor {
1286 // SAFETY: ownership is transferred from stream to f
1287 let f = unsafe { File::from_raw_fd(stream.into_raw_fd()) };
1288 ParcelFileDescriptor::new(f)
1289}
1290
Jiyong Parkdcf17412022-02-08 15:07:23 +09001291/// Parses the platform version requirement string.
1292fn parse_platform_version_req(s: &str) -> Result<VersionReq, Status> {
1293 VersionReq::parse(s).map_err(|e| {
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001294 Status::new_exception_str(
Jiyong Parkdcf17412022-02-08 15:07:23 +09001295 ExceptionCode::BAD_PARCELABLE,
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001296 Some(format!("Invalid platform version requirement {}: {:?}", s, e)),
Jiyong Parkdcf17412022-02-08 15:07:23 +09001297 )
1298 })
1299}
1300
Jooyung Han35edb8f2021-07-01 16:17:16 +09001301/// Simple utility for referencing Borrowed or Owned. Similar to std::borrow::Cow, but
1302/// it doesn't require that T implements Clone.
1303enum BorrowedOrOwned<'a, T> {
1304 Borrowed(&'a T),
1305 Owned(T),
1306}
1307
1308impl<'a, T> AsRef<T> for BorrowedOrOwned<'a, T> {
1309 fn as_ref(&self) -> &T {
1310 match self {
1311 Self::Borrowed(b) => b,
Chris Wailes68c39f82021-07-27 16:03:44 -07001312 Self::Owned(o) => o,
Jooyung Han35edb8f2021-07-01 16:17:16 +09001313 }
1314 }
1315}
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001316
1317/// Implementation of `IVirtualMachineService`, the entry point of the AIDL service.
1318#[derive(Debug, Default)]
1319struct VirtualMachineService {
1320 state: Arc<Mutex<State>>,
Inseob Kimc7d28c72021-10-25 14:28:10 +00001321 cid: Cid,
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001322}
1323
1324impl Interface for VirtualMachineService {}
1325
1326impl IVirtualMachineService for VirtualMachineService {
Inseob Kimc7d28c72021-10-25 14:28:10 +00001327 fn notifyPayloadStarted(&self) -> binder::Result<()> {
1328 let cid = self.cid;
Inseob Kim7f61fe72021-08-20 20:50:47 +09001329 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001330 info!("VM with CID {} started payload", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001331 vm.update_payload_state(PayloadState::Started).map_err(|e| {
1332 Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
1333 })?;
David Brazdil451cc962022-10-14 14:08:12 +01001334 vm.callbacks.notify_payload_started(cid);
Seungjae Yoo62085c02022-08-12 04:44:52 +00001335
Seungjae Yoo6d265d92022-11-15 10:51:33 +09001336 let vm_start_timestamp = vm.vm_metric.lock().unwrap().start_timestamp;
1337 write_vm_booted_stats(vm.requester_uid as i32, &vm.name, vm_start_timestamp);
Inseob Kim7f61fe72021-08-20 20:50:47 +09001338 Ok(())
1339 } else {
Jooyung Handd0a1732021-11-23 15:26:20 +09001340 error!("notifyPayloadStarted is called from an unknown CID {}", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001341 Err(Status::new_service_specific_error_str(
1342 -1,
1343 Some(format!("cannot find a VM with CID {}", cid)),
Inseob Kim7f61fe72021-08-20 20:50:47 +09001344 ))
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001345 }
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001346 }
Inseob Kim2444af92021-08-31 01:22:50 +09001347
Inseob Kimc7d28c72021-10-25 14:28:10 +00001348 fn notifyPayloadReady(&self) -> binder::Result<()> {
1349 let cid = self.cid;
Inseob Kim14cb8692021-08-31 21:50:39 +09001350 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001351 info!("VM with CID {} reported payload is ready", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001352 vm.update_payload_state(PayloadState::Ready).map_err(|e| {
1353 Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
1354 })?;
Inseob Kim14cb8692021-08-31 21:50:39 +09001355 vm.callbacks.notify_payload_ready(cid);
1356 Ok(())
1357 } else {
Jooyung Handd0a1732021-11-23 15:26:20 +09001358 error!("notifyPayloadReady is called from an unknown CID {}", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001359 Err(Status::new_service_specific_error_str(
1360 -1,
1361 Some(format!("cannot find a VM with CID {}", cid)),
Inseob Kim14cb8692021-08-31 21:50:39 +09001362 ))
1363 }
1364 }
1365
Inseob Kimc7d28c72021-10-25 14:28:10 +00001366 fn notifyPayloadFinished(&self, exit_code: i32) -> binder::Result<()> {
1367 let cid = self.cid;
Inseob Kim2444af92021-08-31 01:22:50 +09001368 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001369 info!("VM with CID {} finished payload", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001370 vm.update_payload_state(PayloadState::Finished).map_err(|e| {
1371 Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
1372 })?;
Inseob Kim2444af92021-08-31 01:22:50 +09001373 vm.callbacks.notify_payload_finished(cid, exit_code);
1374 Ok(())
1375 } else {
Jooyung Handd0a1732021-11-23 15:26:20 +09001376 error!("notifyPayloadFinished is called from an unknown CID {}", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001377 Err(Status::new_service_specific_error_str(
1378 -1,
1379 Some(format!("cannot find a VM with CID {}", cid)),
Jooyung Handd0a1732021-11-23 15:26:20 +09001380 ))
1381 }
1382 }
1383
Alan Stokes2bead0d2022-09-05 16:58:34 +01001384 fn notifyError(&self, error_code: ErrorCode, message: &str) -> binder::Result<()> {
Jooyung Handd0a1732021-11-23 15:26:20 +09001385 let cid = self.cid;
1386 if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
David Brazdil415097c2022-10-21 14:17:05 +01001387 info!("VM with CID {} encountered an error", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001388 vm.update_payload_state(PayloadState::Finished).map_err(|e| {
1389 Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
1390 })?;
Jooyung Handd0a1732021-11-23 15:26:20 +09001391 vm.callbacks.notify_error(cid, error_code, message);
1392 Ok(())
1393 } else {
Seungjae Yooec8c1602022-06-20 05:28:00 +00001394 error!("notifyError is called from an unknown CID {}", cid);
Andrew Walbrandcf9d582022-08-03 11:25:24 +00001395 Err(Status::new_service_specific_error_str(
1396 -1,
1397 Some(format!("cannot find a VM with CID {}", cid)),
Inseob Kim2444af92021-08-31 01:22:50 +09001398 ))
1399 }
1400 }
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001401}
1402
1403impl VirtualMachineService {
Inseob Kimc7d28c72021-10-25 14:28:10 +00001404 fn new_binder(state: Arc<Mutex<State>>, cid: Cid) -> Strong<dyn IVirtualMachineService> {
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001405 BnVirtualMachineService::new_binder(
Inseob Kimc7d28c72021-10-25 14:28:10 +00001406 VirtualMachineService { state, cid },
Inseob Kim1b95f2e2021-08-19 13:17:40 +09001407 BinderFeatures::default(),
1408 )
1409 }
1410}
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001411
1412#[cfg(test)]
1413mod tests {
1414 use super::*;
1415
1416 #[test]
1417 fn test_is_allowed_label_for_partition() -> Result<()> {
1418 let expected_results = vec![
1419 ("u:object_r:system_file:s0", true),
1420 ("u:object_r:apk_data_file:s0", true),
1421 ("u:object_r:app_data_file:s0", false),
1422 ("u:object_r:app_data_file:s0:c512,c768", false),
1423 ("u:object_r:privapp_data_file:s0:c512,c768", false),
1424 ("invalid", false),
1425 ("user:role:apk_data_file:severity:categories", true),
1426 ("user:role:apk_data_file:severity:categories:extraneous", false),
1427 ];
1428
1429 for (label, expected_valid) in expected_results {
1430 let context = SeContext::new(label)?;
1431 let result = check_label_is_allowed(&context);
1432 if expected_valid {
1433 assert!(result.is_ok(), "Expected label {} to be allowed, got {:?}", label, result);
1434 } else if result.is_ok() {
1435 bail!("Expected label {} to be disallowed", label);
1436 }
1437 }
1438 Ok(())
1439 }
Nikita Ioffef1ce9872022-12-09 13:31:59 +00001440
1441 #[test]
1442 fn test_create_or_update_idsig_file_empty_apk() -> Result<()> {
1443 let apk = tempfile::tempfile().unwrap();
1444 let idsig = tempfile::tempfile().unwrap();
1445
1446 let ret = create_or_update_idsig_file(
1447 &ParcelFileDescriptor::new(apk),
1448 &ParcelFileDescriptor::new(idsig),
1449 );
1450 assert!(ret.is_err(), "should fail");
1451 Ok(())
1452 }
1453
1454 #[test]
1455 fn test_create_or_update_idsig_dir_instead_of_file_for_apk() -> Result<()> {
1456 let tmp_dir = tempfile::TempDir::new().unwrap();
1457 let apk = File::open(tmp_dir.path()).unwrap();
1458 let idsig = tempfile::tempfile().unwrap();
1459
1460 let ret = create_or_update_idsig_file(
1461 &ParcelFileDescriptor::new(apk),
1462 &ParcelFileDescriptor::new(idsig),
1463 );
1464 assert!(ret.is_err(), "should fail");
1465 Ok(())
1466 }
1467
1468 /// Verifies that create_or_update_idsig_file won't oom if a fd that corresponds to a directory
1469 /// on ext4 filesystem is passed.
1470 /// On ext4 lseek on a directory fd will return (off_t)-1 (see:
1471 /// https://bugzilla.kernel.org/show_bug.cgi?id=200043), which will result in
1472 /// create_or_update_idsig_file ooming while attempting to allocate petabytes of memory.
1473 #[test]
1474 fn test_create_or_update_idsig_does_not_crash_dir_on_ext4() -> Result<()> {
1475 // APEXes are backed by the ext4.
1476 let apk = File::open("/apex/com.android.virt/").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 }
Alan Stokes53cc5ca2022-08-30 14:28:19 +01001486}