blob: 06dd3c6096ef925dd8e442836176a71a770fde2b [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
Jiyong Park21ce2c52021-08-28 02:32:17 +090017mod instance;
Jooyung Hanf48ceb42021-06-01 18:00:04 +090018mod ioutil;
Jooyung Han74573482021-06-08 17:10:21 +090019mod metadata;
Jooyung Han347d9f22021-05-28 00:05:14 +090020
Jiyong Park21ce2c52021-08-28 02:32:17 +090021use crate::instance::InstanceDisk;
Jooyung Han19c1d6c2021-08-06 14:08:16 +090022use anyhow::{anyhow, bail, Context, Result};
23use apkverify::verify;
Inseob Kim1b95f2e2021-08-19 13:17:40 +090024use binder::unstable_api::{new_spibinder, AIBinder};
25use binder::{FromIBinder, Strong};
Jiyong Park21ce2c52021-08-28 02:32:17 +090026use idsig::V4Signature;
27use log::{debug, error, info, warn};
Jooyung Han634e2d72021-06-10 16:27:38 +090028use microdroid_payload_config::{Task, TaskType, VmPayloadConfig};
Inseob Kim7f61fe72021-08-20 20:50:47 +090029use nix::ioctl_read_bad;
Joel Galenson482704c2021-07-29 15:53:53 -070030use rustutils::system_properties::PropertyWatcher;
Inseob Kim7f61fe72021-08-20 20:50:47 +090031use std::fs::{self, File, OpenOptions};
32use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
Jooyung Hanf48ceb42021-06-01 18:00:04 +090033use std::path::Path;
Jiyong Park8611a6c2021-07-09 18:17:44 +090034use std::process::{Command, Stdio};
35use std::str;
Jooyung Han634e2d72021-06-10 16:27:38 +090036use std::time::Duration;
Jiyong Park8611a6c2021-07-09 18:17:44 +090037use vsock::VsockStream;
Jooyung Han634e2d72021-06-10 16:27:38 +090038
Inseob Kim1b95f2e2021-08-19 13:17:40 +090039use android_system_virtualmachineservice::aidl::android::system::virtualmachineservice::IVirtualMachineService::IVirtualMachineService;
40
Jooyung Han634e2d72021-06-10 16:27:38 +090041const WAIT_TIMEOUT: Duration = Duration::from_secs(10);
Jooyung Han19c1d6c2021-08-06 14:08:16 +090042const DM_MOUNTED_APK_PATH: &str = "/dev/block/mapper/microdroid-apk";
Jooyung Han347d9f22021-05-28 00:05:14 +090043
Inseob Kim1b95f2e2021-08-19 13:17:40 +090044/// The CID representing the host VM
45const VMADDR_CID_HOST: u32 = 2;
46
47/// Port number that virtualizationservice listens on connections from the guest VMs for the
48/// VirtualMachineService binder service
49/// Sync with virtualizationservice/src/aidl.rs
50const PORT_VM_BINDER_SERVICE: u32 = 5000;
51
52fn get_vms_rpc_binder() -> Result<Strong<dyn IVirtualMachineService>> {
53 // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership can be
54 // safely taken by new_spibinder.
55 let ibinder = unsafe {
56 new_spibinder(binder_rpc_unstable_bindgen::RpcClient(
57 VMADDR_CID_HOST,
58 PORT_VM_BINDER_SERVICE,
59 ) as *mut AIBinder)
60 };
61 if let Some(ibinder) = ibinder {
62 <dyn IVirtualMachineService>::try_from(ibinder).context("Cannot connect to RPC service")
63 } else {
64 bail!("Invalid raw AIBinder")
65 }
66}
67
Inseob Kim7f61fe72021-08-20 20:50:47 +090068const IOCTL_VM_SOCKETS_GET_LOCAL_CID: usize = 0x7b9;
69ioctl_read_bad!(
70 /// Gets local cid from /dev/vsock
71 vm_sockets_get_local_cid,
72 IOCTL_VM_SOCKETS_GET_LOCAL_CID,
73 u32
74);
75
76// TODO: remove this after VS can check the peer addresses of binder clients
77fn get_local_cid() -> Result<u32> {
78 let f = OpenOptions::new()
79 .read(true)
80 .write(false)
81 .open("/dev/vsock")
82 .context("failed to open /dev/vsock")?;
83 let mut ret = 0;
84 // SAFETY: the kernel only modifies the given u32 integer.
85 unsafe { vm_sockets_get_local_cid(f.as_raw_fd(), &mut ret) }?;
86 Ok(ret)
87}
88
Jooyung Han634e2d72021-06-10 16:27:38 +090089fn main() -> Result<()> {
Jiyong Park79b88012021-06-25 13:06:25 +090090 kernlog::init()?;
Jooyung Han347d9f22021-05-28 00:05:14 +090091 info!("started.");
92
Jooyung Han74573482021-06-08 17:10:21 +090093 let metadata = metadata::load()?;
Jooyung Han19c1d6c2021-08-06 14:08:16 +090094
95 if let Err(err) = verify_payloads() {
Jooyung Hand4e035e2021-08-18 21:19:41 +090096 error!("failed to verify payload: {:#?}", err);
97 return Err(err);
Jooyung Han19c1d6c2021-08-06 14:08:16 +090098 }
99
Jiyong Park21ce2c52021-08-28 02:32:17 +0900100 let mut instance = InstanceDisk::new()?;
101 // TODO(jiyong): the data should have an internal structure
102 if let Some(data) = instance.read_microdroid_data().context("Failed to read identity data")? {
103 debug!("read apk root hash: {}", to_hex_string(&data));
104 //TODO(jiyong) apkdmverity should use this root hash instead of the one read from the idsig
105 //file, if the root hash is found in the instance image.
106 } else {
107 let data = get_apk_roothash()?;
108 debug!("write apk root hash: {}", to_hex_string(&data));
109 instance.write_microdroid_data(data.as_ref()).context("Failed to write identity data")?;
110 }
111
Inseob Kim7f61fe72021-08-20 20:50:47 +0900112 let service = get_vms_rpc_binder().expect("cannot connect to VirtualMachineService");
Inseob Kim1b95f2e2021-08-19 13:17:40 +0900113
Jooyung Han74573482021-06-08 17:10:21 +0900114 if !metadata.payload_config_path.is_empty() {
Jooyung Han634e2d72021-06-10 16:27:38 +0900115 let config = load_config(Path::new(&metadata.payload_config_path))?;
116
Andrew Scull6f3e5fe2021-07-02 12:38:21 +0000117 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 -0700118 if let Err(err) = rustutils::system_properties::write("ro.vmsecret.keymint", fake_secret) {
Andrew Scull6f3e5fe2021-07-02 12:38:21 +0000119 warn!("failed to set ro.vmsecret.keymint: {}", err);
120 }
121
Jooyung Han634e2d72021-06-10 16:27:38 +0900122 // TODO(jooyung): wait until sys.boot_completed?
Jooyung Han347d9f22021-05-28 00:05:14 +0900123 if let Some(main_task) = &config.task {
Inseob Kim7f61fe72021-08-20 20:50:47 +0900124 exec_task(main_task, &service).map_err(|e| {
Jiyong Park8611a6c2021-07-09 18:17:44 +0900125 error!("failed to execute task: {}", e);
126 e
127 })?;
Jooyung Han347d9f22021-05-28 00:05:14 +0900128 }
129 }
130
131 Ok(())
132}
133
Jooyung Han19c1d6c2021-08-06 14:08:16 +0900134// TODO(jooyung): v2/v3 full verification can be slow. Consider multithreading.
135fn verify_payloads() -> Result<()> {
136 // We don't verify APEXes since apexd does.
137
138 // should wait APK to be dm-verity mounted by apkdmverity
139 ioutil::wait_for_file(DM_MOUNTED_APK_PATH, WAIT_TIMEOUT)?;
140 verify(DM_MOUNTED_APK_PATH).context(format!("failed to verify {}", DM_MOUNTED_APK_PATH))?;
141
142 info!("payload verification succeeded.");
143 // TODO(jooyung): collect public keys and store them in instance.img
144 Ok(())
145}
146
Jiyong Park21ce2c52021-08-28 02:32:17 +0900147fn get_apk_roothash() -> Result<Box<[u8]>> {
148 let mut idsig = File::open("/dev/block/by-name/microdroid-apk-idsig")?;
149 let idsig = V4Signature::from(&mut idsig)?;
150 Ok(idsig.hashing_info.raw_root_hash)
151}
152
Jooyung Han634e2d72021-06-10 16:27:38 +0900153fn load_config(path: &Path) -> Result<VmPayloadConfig> {
154 info!("loading config from {:?}...", path);
155 let file = ioutil::wait_for_file(path, WAIT_TIMEOUT)?;
156 Ok(serde_json::from_reader(file)?)
157}
158
Jiyong Park8611a6c2021-07-09 18:17:44 +0900159/// Executes the given task. Stdout of the task is piped into the vsock stream to the
160/// virtualizationservice in the host side.
Inseob Kim7f61fe72021-08-20 20:50:47 +0900161fn exec_task(task: &Task, service: &Strong<dyn IVirtualMachineService>) -> Result<()> {
Jiyong Park8611a6c2021-07-09 18:17:44 +0900162 info!("executing main task {:?}...", task);
Inseob Kim7f61fe72021-08-20 20:50:47 +0900163 let mut child = build_command(task)?.spawn()?;
164
165 info!("notifying payload started");
166 service.notifyPayloadStarted(get_local_cid()? as i32)?;
167
Jiyong Park8611a6c2021-07-09 18:17:44 +0900168 match child.wait()?.code() {
169 Some(0) => {
170 info!("task successfully finished");
171 Ok(())
172 }
173 Some(code) => bail!("task exited with exit code: {}", code),
174 None => bail!("task terminated by signal"),
Jiyong Park038b73e2021-06-16 01:57:02 +0900175 }
Jooyung Han347d9f22021-05-28 00:05:14 +0900176}
Jooyung Han634e2d72021-06-10 16:27:38 +0900177
178fn build_command(task: &Task) -> Result<Command> {
Inseob Kim7f61fe72021-08-20 20:50:47 +0900179 const VMADDR_CID_HOST: u32 = 2;
180 const PORT_VIRT_SVC: u32 = 3000;
181
182 let mut command = match task.type_ {
Jooyung Han634e2d72021-06-10 16:27:38 +0900183 TaskType::Executable => {
184 let mut command = Command::new(&task.command);
185 command.args(&task.args);
186 command
187 }
188 TaskType::MicrodroidLauncher => {
189 let mut command = Command::new("/system/bin/microdroid_launcher");
190 command.arg(find_library_path(&task.command)?).args(&task.args);
191 command
192 }
Inseob Kim7f61fe72021-08-20 20:50:47 +0900193 };
194
195 match VsockStream::connect_with_cid_port(VMADDR_CID_HOST, PORT_VIRT_SVC) {
196 Ok(stream) => {
197 // SAFETY: the ownership of the underlying file descriptor is transferred from stream
198 // to the file object, and then into the Command object. When the command is finished,
199 // the file descriptor is closed.
200 let file = unsafe { File::from_raw_fd(stream.into_raw_fd()) };
201 command
202 .stdin(Stdio::from(file.try_clone()?))
203 .stdout(Stdio::from(file.try_clone()?))
204 .stderr(Stdio::from(file));
205 }
206 Err(e) => {
207 error!("failed to connect to virtualization service: {}", e);
208 // Don't fail hard here. Even if we failed to connect to the virtualizationservice,
209 // we keep executing the task. This can happen if the owner of the VM doesn't register
210 // callback to accept the stream. Use /dev/null as the stream so that the task can
211 // make progress without waiting for someone to consume the output.
212 command.stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null());
213 }
214 }
215
216 Ok(command)
Jooyung Han634e2d72021-06-10 16:27:38 +0900217}
218
219fn find_library_path(name: &str) -> Result<String> {
220 let mut watcher = PropertyWatcher::new("ro.product.cpu.abilist")?;
221 let value = watcher.read(|_name, value| Ok(value.trim().to_string()))?;
222 let abi = value.split(',').next().ok_or_else(|| anyhow!("no abilist"))?;
223 let path = format!("/mnt/apk/lib/{}/{}", abi, name);
224
225 let metadata = fs::metadata(&path)?;
226 if !metadata.is_file() {
227 bail!("{} is not a file", &path);
228 }
229
230 Ok(path)
231}
Jiyong Park21ce2c52021-08-28 02:32:17 +0900232
233fn to_hex_string(buf: &[u8]) -> String {
234 buf.iter().map(|b| format!("{:02X}", b)).collect()
235}