blob: ee0e797f21f24b945b588b62c3587717dbbeb534 [file] [log] [blame]
Jooyung Han347d9f22021-05-28 00:05:14 +09001// 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
Jooyung Hanf48ceb42021-06-01 18:00:04 +090017mod ioutil;
Jooyung Han74573482021-06-08 17:10:21 +090018mod metadata;
Jooyung Han347d9f22021-05-28 00:05:14 +090019
Jooyung Han19c1d6c2021-08-06 14:08:16 +090020use anyhow::{anyhow, bail, Context, Result};
21use apkverify::verify;
Inseob Kim1b95f2e2021-08-19 13:17:40 +090022use binder::unstable_api::{new_spibinder, AIBinder};
23use binder::{FromIBinder, Strong};
Andrew Scull6f3e5fe2021-07-02 12:38:21 +000024use log::{error, info, warn};
Jooyung Han634e2d72021-06-10 16:27:38 +090025use microdroid_payload_config::{Task, TaskType, VmPayloadConfig};
Inseob Kim7f61fe72021-08-20 20:50:47 +090026use nix::ioctl_read_bad;
Joel Galenson482704c2021-07-29 15:53:53 -070027use rustutils::system_properties::PropertyWatcher;
Inseob Kim7f61fe72021-08-20 20:50:47 +090028use std::fs::{self, File, OpenOptions};
29use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
Jooyung Hanf48ceb42021-06-01 18:00:04 +090030use std::path::Path;
Jiyong Park8611a6c2021-07-09 18:17:44 +090031use std::process::{Command, Stdio};
32use std::str;
Jooyung Han634e2d72021-06-10 16:27:38 +090033use std::time::Duration;
Jiyong Park8611a6c2021-07-09 18:17:44 +090034use vsock::VsockStream;
Jooyung Han634e2d72021-06-10 16:27:38 +090035
Inseob Kim1b95f2e2021-08-19 13:17:40 +090036use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::IVirtualMachineService;
37
Jooyung Han634e2d72021-06-10 16:27:38 +090038const WAIT_TIMEOUT: Duration = Duration::from_secs(10);
Jooyung Han19c1d6c2021-08-06 14:08:16 +090039const DM_MOUNTED_APK_PATH: &str = "/dev/block/mapper/microdroid-apk";
Jooyung Han347d9f22021-05-28 00:05:14 +090040
Inseob Kim1b95f2e2021-08-19 13:17:40 +090041/// The CID representing the host VM
42const VMADDR_CID_HOST: u32 = 2;
43
44/// Port number that virtualizationservice listens on connections from the guest VMs for the
45/// VirtualMachineService binder service
46/// Sync with virtualizationservice/src/aidl.rs
47const PORT_VM_BINDER_SERVICE: u32 = 5000;
48
49fn get_vms_rpc_binder() -> Result<Strong<dyn IVirtualMachineService>> {
50 // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership can be
51 // safely taken by new_spibinder.
52 let ibinder = unsafe {
53 new_spibinder(binder_rpc_unstable_bindgen::RpcClient(
54 VMADDR_CID_HOST,
55 PORT_VM_BINDER_SERVICE,
56 ) as *mut AIBinder)
57 };
58 if let Some(ibinder) = ibinder {
59 <dyn IVirtualMachineService>::try_from(ibinder).context("Cannot connect to RPC service")
60 } else {
61 bail!("Invalid raw AIBinder")
62 }
63}
64
Inseob Kim7f61fe72021-08-20 20:50:47 +090065const IOCTL_VM_SOCKETS_GET_LOCAL_CID: usize = 0x7b9;
66ioctl_read_bad!(
67 /// Gets local cid from /dev/vsock
68 vm_sockets_get_local_cid,
69 IOCTL_VM_SOCKETS_GET_LOCAL_CID,
70 u32
71);
72
73// TODO: remove this after VS can check the peer addresses of binder clients
74fn get_local_cid() -> Result<u32> {
75 let f = OpenOptions::new()
76 .read(true)
77 .write(false)
78 .open("/dev/vsock")
79 .context("failed to open /dev/vsock")?;
80 let mut ret = 0;
81 // SAFETY: the kernel only modifies the given u32 integer.
82 unsafe { vm_sockets_get_local_cid(f.as_raw_fd(), &mut ret) }?;
83 Ok(ret)
84}
85
Jooyung Han634e2d72021-06-10 16:27:38 +090086fn main() -> Result<()> {
Jiyong Park79b88012021-06-25 13:06:25 +090087 kernlog::init()?;
Jooyung Han347d9f22021-05-28 00:05:14 +090088 info!("started.");
89
Jooyung Han74573482021-06-08 17:10:21 +090090 let metadata = metadata::load()?;
Jooyung Han19c1d6c2021-08-06 14:08:16 +090091
92 if let Err(err) = verify_payloads() {
Jooyung Hand4e035e2021-08-18 21:19:41 +090093 error!("failed to verify payload: {:#?}", err);
94 return Err(err);
Jooyung Han19c1d6c2021-08-06 14:08:16 +090095 }
96
Inseob Kim7f61fe72021-08-20 20:50:47 +090097 let service = get_vms_rpc_binder().expect("cannot connect to VirtualMachineService");
Inseob Kim1b95f2e2021-08-19 13:17:40 +090098
Jooyung Han74573482021-06-08 17:10:21 +090099 if !metadata.payload_config_path.is_empty() {
Jooyung Han634e2d72021-06-10 16:27:38 +0900100 let config = load_config(Path::new(&metadata.payload_config_path))?;
101
Andrew Scull6f3e5fe2021-07-02 12:38:21 +0000102 let fake_secret = "This is a placeholder for a value that is derived from the images that are loaded in the VM.";
Joel Galenson482704c2021-07-29 15:53:53 -0700103 if let Err(err) = rustutils::system_properties::write("ro.vmsecret.keymint", fake_secret) {
Andrew Scull6f3e5fe2021-07-02 12:38:21 +0000104 warn!("failed to set ro.vmsecret.keymint: {}", err);
105 }
106
Jooyung Han634e2d72021-06-10 16:27:38 +0900107 // TODO(jooyung): wait until sys.boot_completed?
Jooyung Han347d9f22021-05-28 00:05:14 +0900108 if let Some(main_task) = &config.task {
Inseob Kim7f61fe72021-08-20 20:50:47 +0900109 exec_task(main_task, &service).map_err(|e| {
Jiyong Park8611a6c2021-07-09 18:17:44 +0900110 error!("failed to execute task: {}", e);
111 e
112 })?;
Jooyung Han347d9f22021-05-28 00:05:14 +0900113 }
114 }
115
116 Ok(())
117}
118
Jooyung Han19c1d6c2021-08-06 14:08:16 +0900119// TODO(jooyung): v2/v3 full verification can be slow. Consider multithreading.
120fn verify_payloads() -> Result<()> {
121 // We don't verify APEXes since apexd does.
122
123 // should wait APK to be dm-verity mounted by apkdmverity
124 ioutil::wait_for_file(DM_MOUNTED_APK_PATH, WAIT_TIMEOUT)?;
125 verify(DM_MOUNTED_APK_PATH).context(format!("failed to verify {}", DM_MOUNTED_APK_PATH))?;
126
127 info!("payload verification succeeded.");
128 // TODO(jooyung): collect public keys and store them in instance.img
129 Ok(())
130}
131
Jooyung Han634e2d72021-06-10 16:27:38 +0900132fn load_config(path: &Path) -> Result<VmPayloadConfig> {
133 info!("loading config from {:?}...", path);
134 let file = ioutil::wait_for_file(path, WAIT_TIMEOUT)?;
135 Ok(serde_json::from_reader(file)?)
136}
137
Jiyong Park8611a6c2021-07-09 18:17:44 +0900138/// Executes the given task. Stdout of the task is piped into the vsock stream to the
139/// virtualizationservice in the host side.
Inseob Kim7f61fe72021-08-20 20:50:47 +0900140fn exec_task(task: &Task, service: &Strong<dyn IVirtualMachineService>) -> Result<()> {
Jiyong Park8611a6c2021-07-09 18:17:44 +0900141 info!("executing main task {:?}...", task);
Inseob Kim7f61fe72021-08-20 20:50:47 +0900142 let mut child = build_command(task)?.spawn()?;
143
144 info!("notifying payload started");
145 service.notifyPayloadStarted(get_local_cid()? as i32)?;
146
Jiyong Park8611a6c2021-07-09 18:17:44 +0900147 match child.wait()?.code() {
148 Some(0) => {
149 info!("task successfully finished");
150 Ok(())
151 }
152 Some(code) => bail!("task exited with exit code: {}", code),
153 None => bail!("task terminated by signal"),
Jiyong Park038b73e2021-06-16 01:57:02 +0900154 }
Jooyung Han347d9f22021-05-28 00:05:14 +0900155}
Jooyung Han634e2d72021-06-10 16:27:38 +0900156
157fn build_command(task: &Task) -> Result<Command> {
Inseob Kim7f61fe72021-08-20 20:50:47 +0900158 const VMADDR_CID_HOST: u32 = 2;
159 const PORT_VIRT_SVC: u32 = 3000;
160
161 let mut command = match task.type_ {
Jooyung Han634e2d72021-06-10 16:27:38 +0900162 TaskType::Executable => {
163 let mut command = Command::new(&task.command);
164 command.args(&task.args);
165 command
166 }
167 TaskType::MicrodroidLauncher => {
168 let mut command = Command::new("/system/bin/microdroid_launcher");
169 command.arg(find_library_path(&task.command)?).args(&task.args);
170 command
171 }
Inseob Kim7f61fe72021-08-20 20:50:47 +0900172 };
173
174 match VsockStream::connect_with_cid_port(VMADDR_CID_HOST, PORT_VIRT_SVC) {
175 Ok(stream) => {
176 // SAFETY: the ownership of the underlying file descriptor is transferred from stream
177 // to the file object, and then into the Command object. When the command is finished,
178 // the file descriptor is closed.
179 let file = unsafe { File::from_raw_fd(stream.into_raw_fd()) };
180 command
181 .stdin(Stdio::from(file.try_clone()?))
182 .stdout(Stdio::from(file.try_clone()?))
183 .stderr(Stdio::from(file));
184 }
185 Err(e) => {
186 error!("failed to connect to virtualization service: {}", e);
187 // Don't fail hard here. Even if we failed to connect to the virtualizationservice,
188 // we keep executing the task. This can happen if the owner of the VM doesn't register
189 // callback to accept the stream. Use /dev/null as the stream so that the task can
190 // make progress without waiting for someone to consume the output.
191 command.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null());
192 }
193 }
194
195 Ok(command)
Jooyung Han634e2d72021-06-10 16:27:38 +0900196}
197
198fn find_library_path(name: &str) -> Result<String> {
199 let mut watcher = PropertyWatcher::new("ro.product.cpu.abilist")?;
200 let value = watcher.read(|_name, value| Ok(value.trim().to_string()))?;
201 let abi = value.split(',').next().ok_or_else(|| anyhow!("no abilist"))?;
202 let path = format!("/mnt/apk/lib/{}/{}", abi, name);
203
204 let metadata = fs::metadata(&path)?;
205 if !metadata.is_file() {
206 bail!("{} is not a file", &path);
207 }
208
209 Ok(path)
210}