Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 1 | // Copyright 2022, 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 | |
Alice Wang | 59a9e56 | 2022-10-04 15:24:10 +0000 | [diff] [blame] | 15 | //! This module handles the interaction with virtual machine payload service. |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 16 | |
Alice Wang | 13a2b1b | 2022-10-07 07:57:08 +0000 | [diff] [blame] | 17 | use android_system_virtualization_payload::aidl::android::system::virtualization::payload::IVmPayloadService::{ |
Alice Wang | 43c884b | 2022-10-24 09:42:40 +0000 | [diff] [blame] | 18 | IVmPayloadService, VM_PAYLOAD_SERVICE_SOCKET_NAME, VM_APK_CONTENTS_PATH}; |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 19 | use anyhow::{ensure, Context, Result}; |
Alice Wang | 43c884b | 2022-10-24 09:42:40 +0000 | [diff] [blame] | 20 | use binder::{Strong, unstable_api::{AIBinder, new_spibinder}}; |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 21 | use lazy_static::lazy_static; |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 22 | use log::{error, info, Level}; |
David Brazdil | 671e614 | 2022-11-16 11:47:27 +0000 | [diff] [blame] | 23 | use rpcbinder::{get_unix_domain_rpc_interface, RpcServer}; |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 24 | use std::ffi::CString; |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 25 | use std::fmt::Debug; |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 26 | use std::os::raw::{c_char, c_void}; |
Alan Stokes | 78d2470 | 2022-11-21 15:28:31 +0000 | [diff] [blame] | 27 | use std::ptr; |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 28 | use std::sync::{Mutex, atomic::{AtomicBool, Ordering}}; |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 29 | |
| 30 | lazy_static! { |
| 31 | static ref VM_APK_CONTENTS_PATH_C: CString = |
| 32 | CString::new(VM_APK_CONTENTS_PATH).expect("CString::new failed"); |
Alan Stokes | 0cbfdf9 | 2022-11-21 17:17:53 +0000 | [diff] [blame] | 33 | static ref PAYLOAD_CONNECTION: Mutex<Option<Strong<dyn IVmPayloadService>>> = Mutex::default(); |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 34 | } |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 35 | |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 36 | static ALREADY_NOTIFIED: AtomicBool = AtomicBool::new(false); |
| 37 | |
Alan Stokes | 0cbfdf9 | 2022-11-21 17:17:53 +0000 | [diff] [blame] | 38 | /// Return a connection to the payload service in Microdroid Manager. Uses the existing connection |
| 39 | /// if there is one, otherwise attempts to create a new one. |
| 40 | fn get_vm_payload_service() -> Result<Strong<dyn IVmPayloadService>> { |
| 41 | let mut connection = PAYLOAD_CONNECTION.lock().unwrap(); |
| 42 | if let Some(strong) = &*connection { |
| 43 | Ok(strong.clone()) |
| 44 | } else { |
| 45 | let new_connection: Strong<dyn IVmPayloadService> = get_unix_domain_rpc_interface( |
| 46 | VM_PAYLOAD_SERVICE_SOCKET_NAME, |
| 47 | ) |
| 48 | .context(format!("Failed to connect to service: {}", VM_PAYLOAD_SERVICE_SOCKET_NAME))?; |
| 49 | *connection = Some(new_connection.clone()); |
| 50 | Ok(new_connection) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// Make sure our logging goes to logcat. It is harmless to call this more than once. |
Alan Stokes | f30982b | 2022-11-18 11:50:32 +0000 | [diff] [blame] | 55 | fn initialize_logging() { |
| 56 | android_logger::init_once( |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 57 | android_logger::Config::default().with_tag("vm_payload").with_min_level(Level::Info), |
Alan Stokes | f30982b | 2022-11-18 11:50:32 +0000 | [diff] [blame] | 58 | ); |
| 59 | } |
| 60 | |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 61 | /// In many cases clients can't do anything useful if API calls fail, and the failure |
| 62 | /// generally indicates that the VM is exiting or otherwise doomed. So rather than |
| 63 | /// returning a non-actionable error indication we just log the problem and abort |
| 64 | /// the process. |
| 65 | fn unwrap_or_abort<T, E: Debug>(result: Result<T, E>) -> T { |
| 66 | result.unwrap_or_else(|e| { |
| 67 | let msg = format!("{:?}", e); |
| 68 | error!("{msg}"); |
| 69 | panic!("{msg}") |
| 70 | }) |
| 71 | } |
| 72 | |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 73 | /// Notifies the host that the payload is ready. |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 74 | /// Panics on failure. |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 75 | #[no_mangle] |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 76 | pub extern "C" fn AVmPayload_notifyPayloadReady() { |
Alan Stokes | f30982b | 2022-11-18 11:50:32 +0000 | [diff] [blame] | 77 | initialize_logging(); |
| 78 | |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 79 | if !ALREADY_NOTIFIED.swap(true, Ordering::Relaxed) { |
| 80 | unwrap_or_abort(try_notify_payload_ready()); |
| 81 | |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 82 | info!("Notified host payload ready successfully"); |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | /// Notifies the host that the payload is ready. |
| 87 | /// Returns a `Result` containing error information if failed. |
| 88 | fn try_notify_payload_ready() -> Result<()> { |
Alice Wang | 59a9e56 | 2022-10-04 15:24:10 +0000 | [diff] [blame] | 89 | get_vm_payload_service()?.notifyPayloadReady().context("Cannot notify payload ready") |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 92 | /// Runs a binder RPC server, serving the supplied binder service implementation on the given vsock |
| 93 | /// port. |
| 94 | /// |
| 95 | /// If and when the server is ready for connections (it is listening on the port), `on_ready` is |
| 96 | /// called to allow appropriate action to be taken - e.g. to notify clients that they may now |
| 97 | /// attempt to connect. |
| 98 | /// |
| 99 | /// The current thread is joined to the binder thread pool to handle incoming messages. |
| 100 | /// |
| 101 | /// Returns true if the server has shutdown normally, false if it failed in some way. |
| 102 | /// |
| 103 | /// # Safety |
| 104 | /// |
| 105 | /// The `on_ready` callback is only called inside `run_vsock_rpc_server`, within the lifetime of |
| 106 | /// `ReadyNotifier` (the last parameter of `run_vsock_rpc_server`). If `on_ready` is called with |
| 107 | /// wrong param, the callback execution could go wrong. |
| 108 | #[no_mangle] |
| 109 | pub unsafe extern "C" fn AVmPayload_runVsockRpcServer( |
| 110 | service: *mut AIBinder, |
| 111 | port: u32, |
| 112 | on_ready: Option<unsafe extern "C" fn(param: *mut c_void)>, |
| 113 | param: *mut c_void, |
| 114 | ) -> bool { |
Alan Stokes | f30982b | 2022-11-18 11:50:32 +0000 | [diff] [blame] | 115 | initialize_logging(); |
| 116 | |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 117 | // SAFETY: AIBinder returned has correct reference count, and the ownership can |
| 118 | // safely be taken by new_spibinder. |
| 119 | let service = new_spibinder(service); |
| 120 | if let Some(service) = service { |
David Brazdil | 671e614 | 2022-11-16 11:47:27 +0000 | [diff] [blame] | 121 | match RpcServer::new_vsock(service, port) { |
| 122 | Ok(server) => { |
| 123 | if let Some(on_ready) = on_ready { |
| 124 | on_ready(param); |
| 125 | } |
| 126 | server.join(); |
| 127 | true |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 128 | } |
David Brazdil | 671e614 | 2022-11-16 11:47:27 +0000 | [diff] [blame] | 129 | Err(err) => { |
| 130 | error!("Failed to start RpcServer: {:?}", err); |
| 131 | false |
| 132 | } |
| 133 | } |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 134 | } else { |
| 135 | error!("Failed to convert the given service from AIBinder to SpIBinder."); |
| 136 | false |
| 137 | } |
| 138 | } |
| 139 | |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 140 | /// Get a secret that is uniquely bound to this VM instance. |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 141 | /// Panics on failure. |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 142 | /// |
| 143 | /// # Safety |
| 144 | /// |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 145 | /// Behavior is undefined if any of the following conditions are violated: |
| 146 | /// |
| 147 | /// * `identifier` must be [valid] for reads of `identifier_size` bytes. |
| 148 | /// * `secret` must be [valid] for writes of `size` bytes. |
| 149 | /// |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 150 | /// [valid]: ptr#safety |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 151 | #[no_mangle] |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 152 | pub unsafe extern "C" fn AVmPayload_getVmInstanceSecret( |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 153 | identifier: *const u8, |
| 154 | identifier_size: usize, |
| 155 | secret: *mut u8, |
| 156 | size: usize, |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 157 | ) { |
Alan Stokes | f30982b | 2022-11-18 11:50:32 +0000 | [diff] [blame] | 158 | initialize_logging(); |
| 159 | |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 160 | let identifier = std::slice::from_raw_parts(identifier, identifier_size); |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 161 | let vm_secret = unwrap_or_abort(try_get_vm_instance_secret(identifier, size)); |
| 162 | ptr::copy_nonoverlapping(vm_secret.as_ptr(), secret, size); |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | fn try_get_vm_instance_secret(identifier: &[u8], size: usize) -> Result<Vec<u8>> { |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 166 | let vm_secret = get_vm_payload_service()? |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 167 | .getVmInstanceSecret(identifier, i32::try_from(size)?) |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 168 | .context("Cannot get VM instance secret")?; |
| 169 | ensure!( |
| 170 | vm_secret.len() == size, |
| 171 | "Returned secret has {} bytes, expected {}", |
| 172 | vm_secret.len(), |
| 173 | size |
| 174 | ); |
| 175 | Ok(vm_secret) |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 178 | /// Get the VM's attestation chain. |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 179 | /// Panics on failure. |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 180 | /// |
| 181 | /// # Safety |
| 182 | /// |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 183 | /// Behavior is undefined if any of the following conditions are violated: |
| 184 | /// |
| 185 | /// * `data` must be [valid] for writes of `size` bytes. |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 186 | /// |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 187 | /// [valid]: ptr#safety |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 188 | #[no_mangle] |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 189 | pub unsafe extern "C" fn AVmPayload_getDiceAttestationChain(data: *mut u8, size: usize) -> usize { |
Alan Stokes | f30982b | 2022-11-18 11:50:32 +0000 | [diff] [blame] | 190 | initialize_logging(); |
| 191 | |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 192 | let chain = unwrap_or_abort(try_get_dice_attestation_chain()); |
| 193 | ptr::copy_nonoverlapping(chain.as_ptr(), data, std::cmp::min(chain.len(), size)); |
| 194 | chain.len() |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | fn try_get_dice_attestation_chain() -> Result<Vec<u8>> { |
| 198 | get_vm_payload_service()?.getDiceAttestationChain().context("Cannot get attestation chain") |
| 199 | } |
| 200 | |
| 201 | /// Get the VM's attestation CDI. |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 202 | /// Panics on failure. |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 203 | /// |
| 204 | /// # Safety |
| 205 | /// |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 206 | /// Behavior is undefined if any of the following conditions are violated: |
| 207 | /// |
| 208 | /// * `data` must be [valid] for writes of `size` bytes. |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 209 | /// |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 210 | /// [valid]: ptr#safety |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 211 | #[no_mangle] |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 212 | pub unsafe extern "C" fn AVmPayload_getDiceAttestationCdi(data: *mut u8, size: usize) -> usize { |
Alan Stokes | f30982b | 2022-11-18 11:50:32 +0000 | [diff] [blame] | 213 | initialize_logging(); |
| 214 | |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame^] | 215 | let cdi = unwrap_or_abort(try_get_dice_attestation_cdi()); |
| 216 | ptr::copy_nonoverlapping(cdi.as_ptr(), data, std::cmp::min(cdi.len(), size)); |
| 217 | cdi.len() |
| 218 | } |
| 219 | |
| 220 | fn try_get_dice_attestation_cdi() -> Result<Vec<u8>> { |
| 221 | get_vm_payload_service()?.getDiceAttestationCdi().context("Cannot get attestation CDI") |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 224 | /// Gets the path to the APK contents. |
| 225 | #[no_mangle] |
| 226 | pub extern "C" fn AVmPayload_getApkContentsPath() -> *const c_char { |
| 227 | (*VM_APK_CONTENTS_PATH_C).as_ptr() |
| 228 | } |
| 229 | |
Alan Stokes | 78d2470 | 2022-11-21 15:28:31 +0000 | [diff] [blame] | 230 | /// Gets the path to the VM's encrypted storage. |
| 231 | #[no_mangle] |
| 232 | pub extern "C" fn AVmPayload_getEncryptedStoragePath() -> *const c_char { |
| 233 | // TODO(b/254454578): Return a real path if storage is present |
| 234 | ptr::null() |
| 235 | } |