1 // Copyright 2021, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //! Microdroid Manager
16
17 mod dice;
18 mod instance;
19 mod ioutil;
20 mod payload;
21 mod swap;
22 mod verify;
23 mod vm_payload_service;
24 mod vm_secret;
25
26 use android_system_virtualizationcommon::aidl::android::system::virtualizationcommon::ErrorCode::ErrorCode;
27 use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::IVirtualMachineService;
28 use android_system_virtualization_payload::aidl::android::system::virtualization::payload::IVmPayloadService::{
29 VM_APK_CONTENTS_PATH,
30 VM_PAYLOAD_SERVICE_SOCKET_NAME,
31 ENCRYPTEDSTORE_MOUNTPOINT,
32 };
33
34 use crate::dice::dice_derivation;
35 use crate::instance::{InstanceDisk, MicrodroidData};
36 use crate::verify::verify_payload;
37 use crate::vm_payload_service::register_vm_payload_service;
38 use anyhow::{anyhow, bail, ensure, Context, Error, Result};
39 use binder::Strong;
40 use dice_driver::DiceDriver;
41 use keystore2_crypto::ZVec;
42 use libc::VMADDR_CID_HOST;
43 use log::{error, info};
44 use microdroid_metadata::{Metadata, PayloadMetadata};
45 use microdroid_payload_config::{ApkConfig, OsConfig, Task, TaskType, VmPayloadConfig};
46 use nix::mount::{umount2, MntFlags};
47 use nix::sys::signal::Signal;
48 use payload::load_metadata;
49 use rpcbinder::RpcSession;
50 use rustutils::sockets::android_get_control_socket;
51 use rustutils::system_properties;
52 use rustutils::system_properties::PropertyWatcher;
53 use secretkeeper_comm::data_types::ID_SIZE;
54 use std::borrow::Cow::{Borrowed, Owned};
55 use std::env;
56 use std::ffi::CString;
57 use std::fs::{self, create_dir, File, OpenOptions};
58 use std::io::{Read, Write};
59 use std::os::unix::io::{FromRawFd, OwnedFd};
60 use std::os::unix::process::CommandExt;
61 use std::os::unix::process::ExitStatusExt;
62 use std::path::Path;
63 use std::process::{Child, Command, Stdio};
64 use std::str;
65 use std::time::Duration;
66 use vm_secret::VmSecret;
67
68 const WAIT_TIMEOUT: Duration = Duration::from_secs(10);
69 const AVF_STRICT_BOOT: &str = "/proc/device-tree/chosen/avf,strict-boot";
70 const AVF_NEW_INSTANCE: &str = "/proc/device-tree/chosen/avf,new-instance";
71 const AVF_DEBUG_POLICY_RAMDUMP: &str = "/proc/device-tree/avf/guest/common/ramdump";
72 const DEBUG_MICRODROID_NO_VERIFIED_BOOT: &str =
73 "/proc/device-tree/virtualization/guest/debug-microdroid,no-verified-boot";
74 const SECRETKEEPER_KEY: &str = "/proc/device-tree/avf/secretkeeper_public_key";
75 const INSTANCE_ID_PATH: &str = "/proc/device-tree/avf/untrusted/instance-id";
76 const DEFER_ROLLBACK_PROTECTION: &str = "/proc/device-tree/avf/untrusted/defer-rollback-protection";
77
78 const ENCRYPTEDSTORE_BIN: &str = "/system/bin/encryptedstore";
79 const ZIPFUSE_BIN: &str = "/system/bin/zipfuse";
80
81 const APEX_CONFIG_DONE_PROP: &str = "apex_config.done";
82 const DEBUGGABLE_PROP: &str = "ro.boot.microdroid.debuggable";
83
84 // SYNC WITH virtualizationservice/src/crosvm.rs
85 const FAILURE_SERIAL_DEVICE: &str = "/dev/ttyS1";
86
87 const ENCRYPTEDSTORE_BACKING_DEVICE: &str = "/dev/block/by-name/encryptedstore";
88 const ENCRYPTEDSTORE_KEYSIZE: usize = 32;
89
90 const DICE_CHAIN_FILE: &str = "/microdroid_resources/dice_chain.raw";
91
92 #[derive(thiserror::Error, Debug)]
93 enum MicrodroidError {
94 #[error("Cannot connect to virtualization service: {0}")]
95 FailedToConnectToVirtualizationService(String),
96 #[error("Payload has changed: {0}")]
97 PayloadChanged(String),
98 #[error("Payload verification has failed: {0}")]
99 PayloadVerificationFailed(String),
100 #[error("Payload config is invalid: {0}")]
101 PayloadInvalidConfig(String),
102 }
103
translate_error(err: &Error) -> (ErrorCode, String)104 fn translate_error(err: &Error) -> (ErrorCode, String) {
105 if let Some(e) = err.downcast_ref::<MicrodroidError>() {
106 match e {
107 MicrodroidError::PayloadChanged(msg) => (ErrorCode::PAYLOAD_CHANGED, msg.to_string()),
108 MicrodroidError::PayloadVerificationFailed(msg) => {
109 (ErrorCode::PAYLOAD_VERIFICATION_FAILED, msg.to_string())
110 }
111 MicrodroidError::PayloadInvalidConfig(msg) => {
112 (ErrorCode::PAYLOAD_INVALID_CONFIG, msg.to_string())
113 }
114 // Connection failure won't be reported to VS; return the default value
115 MicrodroidError::FailedToConnectToVirtualizationService(msg) => {
116 (ErrorCode::UNKNOWN, msg.to_string())
117 }
118 }
119 } else {
120 (ErrorCode::UNKNOWN, err.to_string())
121 }
122 }
123
write_death_reason_to_serial(err: &Error) -> Result<()>124 fn write_death_reason_to_serial(err: &Error) -> Result<()> {
125 let death_reason = if let Some(e) = err.downcast_ref::<MicrodroidError>() {
126 Borrowed(match e {
127 MicrodroidError::FailedToConnectToVirtualizationService(_) => {
128 "MICRODROID_FAILED_TO_CONNECT_TO_VIRTUALIZATION_SERVICE"
129 }
130 MicrodroidError::PayloadChanged(_) => "MICRODROID_PAYLOAD_HAS_CHANGED",
131 MicrodroidError::PayloadVerificationFailed(_) => {
132 "MICRODROID_PAYLOAD_VERIFICATION_FAILED"
133 }
134 MicrodroidError::PayloadInvalidConfig(_) => "MICRODROID_INVALID_PAYLOAD_CONFIG",
135 })
136 } else {
137 // Send context information back after a separator, to ease diagnosis.
138 // These errors occur before the payload runs, so this should not leak sensitive
139 // information.
140 Owned(format!("MICRODROID_UNKNOWN_RUNTIME_ERROR|{:?}", err))
141 };
142
143 for chunk in death_reason.as_bytes().chunks(16) {
144 // TODO(b/220071963): Sometimes, sending more than 16 bytes at once makes MM hang.
145 OpenOptions::new().read(false).write(true).open(FAILURE_SERIAL_DEVICE)?.write_all(chunk)?;
146 }
147
148 Ok(())
149 }
150
151 /// The (host allocated) instance_id can be found at node /avf/untrusted/ in the device tree.
get_instance_id() -> Result<Option<[u8; ID_SIZE]>>152 fn get_instance_id() -> Result<Option<[u8; ID_SIZE]>> {
153 let path = Path::new(INSTANCE_ID_PATH);
154 let instance_id = if path.exists() {
155 Some(
156 fs::read(path)?
157 .try_into()
158 .map_err(|x: Vec<_>| anyhow!("Expected {ID_SIZE} bytes, found {:?}", x.len()))?,
159 )
160 } else {
161 // TODO(b/325094712): x86 support for Device tree in nested guest is limited/broken/
162 // untested. So instance_id will not be present in cuttlefish.
163 None
164 };
165 Ok(instance_id)
166 }
167
should_defer_rollback_protection() -> bool168 fn should_defer_rollback_protection() -> bool {
169 Path::new(DEFER_ROLLBACK_PROTECTION).exists()
170 }
171
main() -> Result<()>172 fn main() -> Result<()> {
173 // If debuggable, print full backtrace to console log with stdio_to_kmsg
174 if is_debuggable()? {
175 env::set_var("RUST_BACKTRACE", "full");
176 }
177
178 scopeguard::defer! {
179 info!("Shutting down...");
180 if let Err(e) = system_properties::write("sys.powerctl", "shutdown") {
181 error!("failed to shutdown {:?}", e);
182 }
183 }
184
185 try_main().map_err(|e| {
186 error!("Failed with {:?}.", e);
187 if let Err(e) = write_death_reason_to_serial(&e) {
188 error!("Failed to write death reason {:?}", e);
189 }
190 e
191 })
192 }
193
try_main() -> Result<()>194 fn try_main() -> Result<()> {
195 android_logger::init_once(
196 android_logger::Config::default()
197 .with_tag("microdroid_manager")
198 .with_max_level(log::LevelFilter::Info),
199 );
200 info!("started.");
201
202 // SAFETY: This is the only place we take the ownership of the fd of the vm payload service.
203 //
204 // To ensure that the CLOEXEC flag is set on the file descriptor as early as possible,
205 // it is necessary to fetch the socket corresponding to vm_payload_service at the
206 // very beginning, as android_get_control_socket() sets the CLOEXEC flag on the file
207 // descriptor.
208 let vm_payload_service_fd = unsafe { prepare_vm_payload_service_socket()? };
209
210 load_crashkernel_if_supported().context("Failed to load crashkernel")?;
211
212 swap::init_swap().context("Failed to initialize swap")?;
213 info!("swap enabled.");
214
215 let service = get_vms_rpc_binder()
216 .context("cannot connect to VirtualMachineService")
217 .map_err(|e| MicrodroidError::FailedToConnectToVirtualizationService(e.to_string()))?;
218
219 match try_run_payload(&service, vm_payload_service_fd) {
220 Ok(code) => {
221 if code == 0 {
222 info!("task successfully finished");
223 } else {
224 error!("task exited with exit code: {}", code);
225 }
226 if let Err(e) = post_payload_work() {
227 error!(
228 "Failed to run post payload work. It is possible that certain tasks
229 like syncing encrypted store might be incomplete. Error: {:?}",
230 e
231 );
232 };
233
234 info!("notifying payload finished");
235 service.notifyPayloadFinished(code)?;
236 Ok(())
237 }
238 Err(err) => {
239 let (error_code, message) = translate_error(&err);
240 service.notifyError(error_code, &message)?;
241 Err(err)
242 }
243 }
244 }
245
verify_payload_with_instance_img( metadata: &Metadata, dice: &DiceDriver, ) -> Result<MicrodroidData>246 fn verify_payload_with_instance_img(
247 metadata: &Metadata,
248 dice: &DiceDriver,
249 ) -> Result<MicrodroidData> {
250 let mut instance = InstanceDisk::new().context("Failed to load instance.img")?;
251 let saved_data = instance.read_microdroid_data(dice).context("Failed to read identity data")?;
252
253 if is_strict_boot() {
254 // Provisioning must happen on the first boot and never again.
255 if is_new_instance() {
256 ensure!(
257 saved_data.is_none(),
258 MicrodroidError::PayloadInvalidConfig(
259 "Found instance data on first boot.".to_string()
260 )
261 );
262 } else {
263 ensure!(
264 saved_data.is_some(),
265 MicrodroidError::PayloadInvalidConfig("Instance data not found.".to_string())
266 );
267 };
268 }
269
270 // Verify the payload before using it.
271 let extracted_data = verify_payload(metadata, saved_data.as_ref())
272 .context("Payload verification failed")
273 .map_err(|e| MicrodroidError::PayloadVerificationFailed(e.to_string()))?;
274
275 // In case identity is ignored (by debug policy), we should reuse existing payload data, even
276 // when the payload is changed. This is to keep the derived secret same as before.
277 let instance_data = if let Some(saved_data) = saved_data {
278 if !is_verified_boot() {
279 if saved_data != extracted_data {
280 info!("Detected an update of the payload, but continue (regarding debug policy)")
281 }
282 } else {
283 ensure!(
284 saved_data == extracted_data,
285 MicrodroidError::PayloadChanged(String::from(
286 "Detected an update of the payload which isn't supported yet."
287 ))
288 );
289 info!("Saved data is verified.");
290 }
291 saved_data
292 } else {
293 info!("Saving verified data.");
294 instance
295 .write_microdroid_data(&extracted_data, dice)
296 .context("Failed to write identity data")?;
297 extracted_data
298 };
299 Ok(instance_data)
300 }
301
try_run_payload( service: &Strong<dyn IVirtualMachineService>, vm_payload_service_fd: OwnedFd, ) -> Result<i32>302 fn try_run_payload(
303 service: &Strong<dyn IVirtualMachineService>,
304 vm_payload_service_fd: OwnedFd,
305 ) -> Result<i32> {
306 let metadata = load_metadata().context("Failed to load payload metadata")?;
307 let dice = if Path::new(DICE_CHAIN_FILE).exists() {
308 DiceDriver::from_file(Path::new(DICE_CHAIN_FILE))
309 .context("Failed to load DICE from file")?
310 } else {
311 DiceDriver::new(Path::new("/dev/open-dice0"), is_strict_boot())
312 .context("Failed to load DICE from driver")?
313 };
314
315 // Microdroid skips checking payload against instance image iff the device supports
316 // secretkeeper. In that case Microdroid use VmSecret::V2, which provide protection against
317 // rollback of boot images and packages.
318 let instance_data = if should_defer_rollback_protection() {
319 verify_payload(&metadata, None)?
320 } else {
321 verify_payload_with_instance_img(&metadata, &dice)?
322 };
323
324 let payload_metadata = metadata.payload.ok_or_else(|| {
325 MicrodroidError::PayloadInvalidConfig("No payload config in metadata".to_string())
326 })?;
327
328 // To minimize the exposure to untrusted data, derive dice profile as soon as possible.
329 info!("DICE derivation for payload");
330 let dice_artifacts = dice_derivation(dice, &instance_data, &payload_metadata)?;
331 let vm_secret =
332 VmSecret::new(dice_artifacts, service).context("Failed to create VM secrets")?;
333
334 if cfg!(dice_changes) {
335 // Now that the DICE derivation is done, it's ok to allow payload code to run.
336
337 // Start apexd to activate APEXes. This may allow code within them to run.
338 system_properties::write("ctl.start", "apexd-vm")?;
339
340 // Unmounting /microdroid_resources is a defence-in-depth effort to ensure that payload
341 // can't get hold of dice chain stored there.
342 umount2("/microdroid_resources", MntFlags::MNT_DETACH)?;
343 }
344
345 // Run encryptedstore binary to prepare the storage
346 let encryptedstore_child = if Path::new(ENCRYPTEDSTORE_BACKING_DEVICE).exists() {
347 info!("Preparing encryptedstore ...");
348 Some(prepare_encryptedstore(&vm_secret).context("encryptedstore run")?)
349 } else {
350 None
351 };
352
353 let mut zipfuse = Zipfuse::default();
354
355 // Before reading a file from the APK, start zipfuse
356 zipfuse.mount(
357 MountForExec::Allowed,
358 "fscontext=u:object_r:zipfusefs:s0,context=u:object_r:system_file:s0",
359 Path::new(verify::DM_MOUNTED_APK_PATH),
360 Path::new(VM_APK_CONTENTS_PATH),
361 "microdroid_manager.apk.mounted".to_owned(),
362 )?;
363
364 // Restricted APIs are only allowed to be used by platform or test components. Infer this from
365 // the use of a VM config file since those can only be used by platform and test components.
366 let allow_restricted_apis = match payload_metadata {
367 PayloadMetadata::ConfigPath(_) => true,
368 PayloadMetadata::Config(_) => false,
369 _ => false, // default is false for safety
370 };
371
372 let config = load_config(payload_metadata).context("Failed to load payload metadata")?;
373
374 let task = config
375 .task
376 .as_ref()
377 .ok_or_else(|| MicrodroidError::PayloadInvalidConfig("No task in VM config".to_string()))?;
378
379 ensure!(
380 config.extra_apks.len() == instance_data.extra_apks_data.len(),
381 "config expects {} extra apks, but found {}",
382 config.extra_apks.len(),
383 instance_data.extra_apks_data.len()
384 );
385 mount_extra_apks(&config, &mut zipfuse)?;
386
387 register_vm_payload_service(
388 allow_restricted_apis,
389 service.clone(),
390 vm_secret,
391 vm_payload_service_fd,
392 )?;
393
394 // Set export_tombstones if enabled
395 if should_export_tombstones(&config) {
396 // This property is read by tombstone_handler.
397 system_properties::write("microdroid_manager.export_tombstones.enabled", "1")
398 .context("set microdroid_manager.export_tombstones.enabled")?;
399 }
400
401 // Wait until apex config is done. (e.g. linker configuration for apexes)
402 wait_for_property_true(APEX_CONFIG_DONE_PROP).context("Failed waiting for apex config done")?;
403
404 // Trigger init post-fs-data. This will start authfs if we wask it to.
405 if config.enable_authfs {
406 system_properties::write("microdroid_manager.authfs.enabled", "1")
407 .context("failed to write microdroid_manager.authfs.enabled")?;
408 }
409 system_properties::write("microdroid_manager.config_done", "1")
410 .context("failed to write microdroid_manager.config_done")?;
411
412 // Wait until zipfuse has mounted the APKs so we can access the payload
413 zipfuse.wait_until_done()?;
414
415 // Wait for encryptedstore to finish mounting the storage (if enabled) before setting
416 // microdroid_manager.init_done. Reason is init stops uneventd after that.
417 // Encryptedstore, however requires ueventd
418 if let Some(mut child) = encryptedstore_child {
419 let exitcode = child.wait().context("Wait for encryptedstore child")?;
420 ensure!(exitcode.success(), "Unable to prepare encrypted storage. Exitcode={}", exitcode);
421 }
422
423 // Wait for init to have finished booting.
424 wait_for_property_true("dev.bootcomplete").context("failed waiting for dev.bootcomplete")?;
425
426 // And then tell it we're done so unnecessary services can be shut down.
427 system_properties::write("microdroid_manager.init_done", "1")
428 .context("set microdroid_manager.init_done")?;
429
430 info!("boot completed, time to run payload");
431 exec_task(task, service).context("Failed to run payload")
432 }
433
post_payload_work() -> Result<()>434 fn post_payload_work() -> Result<()> {
435 // Sync the encrypted storage filesystem (flushes the filesystem caches).
436 if Path::new(ENCRYPTEDSTORE_BACKING_DEVICE).exists() {
437 let mountpoint = CString::new(ENCRYPTEDSTORE_MOUNTPOINT).unwrap();
438
439 // SAFETY: `mountpoint` is a valid C string. `syncfs` and `close` are safe for any parameter
440 // values.
441 let ret = unsafe {
442 let dirfd = libc::open(
443 mountpoint.as_ptr(),
444 libc::O_DIRECTORY | libc::O_RDONLY | libc::O_CLOEXEC,
445 );
446 ensure!(dirfd >= 0, "Unable to open {:?}", mountpoint);
447 let ret = libc::syncfs(dirfd);
448 libc::close(dirfd);
449 ret
450 };
451 if ret != 0 {
452 error!("failed to sync encrypted storage.");
453 return Err(anyhow!(std::io::Error::last_os_error()));
454 }
455 }
456 Ok(())
457 }
458
mount_extra_apks(config: &VmPayloadConfig, zipfuse: &mut Zipfuse) -> Result<()>459 fn mount_extra_apks(config: &VmPayloadConfig, zipfuse: &mut Zipfuse) -> Result<()> {
460 // For now, only the number of apks is important, as the mount point and dm-verity name is fixed
461 for i in 0..config.extra_apks.len() {
462 let mount_dir = format!("/mnt/extra-apk/{i}");
463 create_dir(Path::new(&mount_dir)).context("Failed to create mount dir for extra apks")?;
464
465 let mount_for_exec =
466 if cfg!(multi_tenant) { MountForExec::Allowed } else { MountForExec::Disallowed };
467 // These run asynchronously in parallel - we wait later for them to complete.
468 zipfuse.mount(
469 mount_for_exec,
470 "fscontext=u:object_r:zipfusefs:s0,context=u:object_r:extra_apk_file:s0",
471 Path::new(&format!("/dev/block/mapper/extra-apk-{i}")),
472 Path::new(&mount_dir),
473 format!("microdroid_manager.extra_apk.mounted.{i}"),
474 )?;
475 }
476
477 Ok(())
478 }
479
get_vms_rpc_binder() -> Result<Strong<dyn IVirtualMachineService>>480 fn get_vms_rpc_binder() -> Result<Strong<dyn IVirtualMachineService>> {
481 // The host is running a VirtualMachineService for this VM on a port equal
482 // to the CID of this VM.
483 let port = vsock::get_local_cid().context("Could not determine local CID")?;
484 RpcSession::new()
485 .setup_vsock_client(VMADDR_CID_HOST, port)
486 .context("Could not connect to IVirtualMachineService")
487 }
488
489 /// Prepares a socket file descriptor for the vm payload service.
490 ///
491 /// # Safety
492 ///
493 /// The caller must ensure that this function is the only place that claims ownership
494 /// of the file descriptor and it is called only once.
prepare_vm_payload_service_socket() -> Result<OwnedFd>495 unsafe fn prepare_vm_payload_service_socket() -> Result<OwnedFd> {
496 let raw_fd = android_get_control_socket(VM_PAYLOAD_SERVICE_SOCKET_NAME)?;
497
498 // Creating OwnedFd for stdio FDs is not safe.
499 if [libc::STDIN_FILENO, libc::STDOUT_FILENO, libc::STDERR_FILENO].contains(&raw_fd) {
500 bail!("File descriptor {raw_fd} is standard I/O descriptor");
501 }
502 // SAFETY: Initializing OwnedFd for a RawFd created by the init.
503 // We checked that the integer value corresponds to a valid FD and that the caller
504 // ensures that this is the only place to claim its ownership.
505 Ok(unsafe { OwnedFd::from_raw_fd(raw_fd) })
506 }
507
is_strict_boot() -> bool508 fn is_strict_boot() -> bool {
509 Path::new(AVF_STRICT_BOOT).exists()
510 }
511
is_new_instance() -> bool512 fn is_new_instance() -> bool {
513 Path::new(AVF_NEW_INSTANCE).exists()
514 }
515
is_verified_boot() -> bool516 fn is_verified_boot() -> bool {
517 !Path::new(DEBUG_MICRODROID_NO_VERIFIED_BOOT).exists()
518 }
519
is_debuggable() -> Result<bool>520 fn is_debuggable() -> Result<bool> {
521 Ok(system_properties::read_bool(DEBUGGABLE_PROP, true)?)
522 }
523
should_export_tombstones(config: &VmPayloadConfig) -> bool524 fn should_export_tombstones(config: &VmPayloadConfig) -> bool {
525 match config.export_tombstones {
526 Some(b) => b,
527 None => is_debuggable().unwrap_or(false),
528 }
529 }
530
531 /// Get debug policy value in bool. It's true iff the value is explicitly set to <1>.
get_debug_policy_bool(path: &'static str) -> Result<Option<bool>>532 fn get_debug_policy_bool(path: &'static str) -> Result<Option<bool>> {
533 let mut file = match File::open(path) {
534 Ok(dp) => dp,
535 Err(e) => {
536 info!(
537 "Assumes that debug policy is disabled because failed to read debug policy ({e:?})"
538 );
539 return Ok(Some(false));
540 }
541 };
542 let mut log: [u8; 4] = Default::default();
543 file.read_exact(&mut log).context("Malformed data in {path}")?;
544 // DT spec uses big endian although Android is always little endian.
545 Ok(Some(u32::from_be_bytes(log) == 1))
546 }
547
548 enum MountForExec {
549 Allowed,
550 Disallowed,
551 }
552
553 #[derive(Default)]
554 struct Zipfuse {
555 ready_properties: Vec<String>,
556 }
557
558 impl Zipfuse {
mount( &mut self, noexec: MountForExec, option: &str, zip_path: &Path, mount_dir: &Path, ready_prop: String, ) -> Result<Child>559 fn mount(
560 &mut self,
561 noexec: MountForExec,
562 option: &str,
563 zip_path: &Path,
564 mount_dir: &Path,
565 ready_prop: String,
566 ) -> Result<Child> {
567 let mut cmd = Command::new(ZIPFUSE_BIN);
568 if let MountForExec::Disallowed = noexec {
569 cmd.arg("--noexec");
570 }
571 // Let root own the files in APK, so we can access them, but set the group to
572 // allow all payloads to have access too.
573 let (uid, gid) = (microdroid_uids::ROOT_UID, microdroid_uids::MICRODROID_PAYLOAD_GID);
574
575 cmd.args(["-p", &ready_prop, "-o", option]);
576 cmd.args(["-u", &uid.to_string()]);
577 cmd.args(["-g", &gid.to_string()]);
578 cmd.arg(zip_path).arg(mount_dir);
579 self.ready_properties.push(ready_prop);
580 cmd.spawn().with_context(|| format!("Failed to run zipfuse for {mount_dir:?}"))
581 }
582
wait_until_done(self) -> Result<()>583 fn wait_until_done(self) -> Result<()> {
584 // We check the last-started check first in the hope that by the time it is done
585 // all or most of the others will also be done, minimising the number of times we
586 // block on a property.
587 for property in self.ready_properties.into_iter().rev() {
588 wait_for_property_true(&property)
589 .with_context(|| format!("Failed waiting for {property}"))?;
590 }
591 Ok(())
592 }
593 }
594
wait_for_property_true(property_name: &str) -> Result<()>595 fn wait_for_property_true(property_name: &str) -> Result<()> {
596 let mut prop = PropertyWatcher::new(property_name)?;
597 loop {
598 prop.wait(None)?;
599 if system_properties::read_bool(property_name, false)? {
600 break;
601 }
602 }
603 Ok(())
604 }
605
load_config(payload_metadata: PayloadMetadata) -> Result<VmPayloadConfig>606 fn load_config(payload_metadata: PayloadMetadata) -> Result<VmPayloadConfig> {
607 match payload_metadata {
608 PayloadMetadata::ConfigPath(path) => {
609 let path = Path::new(&path);
610 info!("loading config from {:?}...", path);
611 let file = ioutil::wait_for_file(path, WAIT_TIMEOUT)
612 .with_context(|| format!("Failed to read {:?}", path))?;
613 Ok(serde_json::from_reader(file)?)
614 }
615 PayloadMetadata::Config(payload_config) => {
616 let task = Task {
617 type_: TaskType::MicrodroidLauncher,
618 command: payload_config.payload_binary_name,
619 };
620 // We don't care about the paths, only the number of extra APKs really matters.
621 let extra_apks = (0..payload_config.extra_apk_count)
622 .map(|i| ApkConfig { path: format!("extra-apk-{i}") })
623 .collect();
624 Ok(VmPayloadConfig {
625 os: OsConfig { name: "microdroid".to_owned() },
626 task: Some(task),
627 apexes: vec![],
628 extra_apks,
629 prefer_staged: false,
630 export_tombstones: None,
631 enable_authfs: false,
632 hugepages: false,
633 })
634 }
635 _ => bail!("Failed to match config against a config type."),
636 }
637 }
638
639 /// Loads the crashkernel into memory using kexec if debuggable or debug policy says so.
640 /// The VM should be loaded with `crashkernel=' parameter in the cmdline to allocate memory
641 /// for crashkernel.
load_crashkernel_if_supported() -> Result<()>642 fn load_crashkernel_if_supported() -> Result<()> {
643 let supported = std::fs::read_to_string("/proc/cmdline")?.contains(" crashkernel=");
644 info!("ramdump supported: {}", supported);
645
646 if !supported {
647 return Ok(());
648 }
649
650 let debuggable = is_debuggable()?;
651 let ramdump = get_debug_policy_bool(AVF_DEBUG_POLICY_RAMDUMP)?.unwrap_or_default();
652 let requested = debuggable | ramdump;
653
654 if requested {
655 let status = Command::new("/system/bin/kexec_load").status()?;
656 if !status.success() {
657 return Err(anyhow!("Failed to load crashkernel: {:?}", status));
658 }
659 info!("ramdump is loaded: debuggable={debuggable}, ramdump={ramdump}");
660 }
661 Ok(())
662 }
663
664 /// Executes the given task.
exec_task(task: &Task, service: &Strong<dyn IVirtualMachineService>) -> Result<i32>665 fn exec_task(task: &Task, service: &Strong<dyn IVirtualMachineService>) -> Result<i32> {
666 info!("executing main task {:?}...", task);
667 let mut command = match task.type_ {
668 TaskType::Executable => {
669 // TODO(b/297501338): Figure out how to handle non-root for system payloads.
670 Command::new(&task.command)
671 }
672 TaskType::MicrodroidLauncher => {
673 let mut command = Command::new("/system/bin/microdroid_launcher");
674 command.arg(find_library_path(&task.command)?);
675 command.uid(microdroid_uids::MICRODROID_PAYLOAD_UID);
676 command.gid(microdroid_uids::MICRODROID_PAYLOAD_GID);
677 command
678 }
679 };
680
681 // SAFETY: We are not accessing any resource of the parent process. This means we can't make any
682 // log calls inside the closure.
683 unsafe {
684 command.pre_exec(|| {
685 // It is OK to continue with payload execution even if the calls below fail, since
686 // whether process can use a capability is controlled by the SELinux. Dropping the
687 // capabilities here is just another defense-in-depth layer.
688 let _ = cap::drop_inheritable_caps();
689 let _ = cap::drop_bounding_set();
690 Ok(())
691 });
692 }
693
694 command.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null());
695
696 info!("notifying payload started");
697 service.notifyPayloadStarted()?;
698
699 let exit_status = command.spawn()?.wait()?;
700 match exit_status.code() {
701 Some(exit_code) => Ok(exit_code),
702 None => Err(match exit_status.signal() {
703 Some(signal) => anyhow!(
704 "Payload exited due to signal: {} ({})",
705 signal,
706 Signal::try_from(signal).map_or("unknown", |s| s.as_str())
707 ),
708 None => anyhow!("Payload has neither exit code nor signal"),
709 }),
710 }
711 }
712
find_library_path(name: &str) -> Result<String>713 fn find_library_path(name: &str) -> Result<String> {
714 let mut watcher = PropertyWatcher::new("ro.product.cpu.abilist")?;
715 let value = watcher.read(|_name, value| Ok(value.trim().to_string()))?;
716 let abi = value.split(',').next().ok_or_else(|| anyhow!("no abilist"))?;
717 let path = format!("{}/lib/{}/{}", VM_APK_CONTENTS_PATH, abi, name);
718
719 let metadata = fs::metadata(&path).with_context(|| format!("Unable to access {}", path))?;
720 if !metadata.is_file() {
721 bail!("{} is not a file", &path);
722 }
723
724 Ok(path)
725 }
726
prepare_encryptedstore(vm_secret: &VmSecret) -> Result<Child>727 fn prepare_encryptedstore(vm_secret: &VmSecret) -> Result<Child> {
728 let mut key = ZVec::new(ENCRYPTEDSTORE_KEYSIZE)?;
729 vm_secret.derive_encryptedstore_key(&mut key)?;
730 let mut cmd = Command::new(ENCRYPTEDSTORE_BIN);
731 cmd.arg("--blkdevice")
732 .arg(ENCRYPTEDSTORE_BACKING_DEVICE)
733 .arg("--key")
734 .arg(hex::encode(&*key))
735 .args(["--mountpoint", ENCRYPTEDSTORE_MOUNTPOINT])
736 .spawn()
737 .context("encryptedstore failed")
738 }
739