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 | |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 17 | // We're implementing unsafe functions, but we still want warnings on unsafe usage within them. |
| 18 | #![warn(unsafe_op_in_unsafe_fn)] |
| 19 | |
Alice Wang | 13a2b1b | 2022-10-07 07:57:08 +0000 | [diff] [blame] | 20 | use android_system_virtualization_payload::aidl::android::system::virtualization::payload::IVmPayloadService::{ |
Shikha Panwar | ddc124b | 2022-11-28 19:17:54 +0000 | [diff] [blame] | 21 | ENCRYPTEDSTORE_MOUNTPOINT, IVmPayloadService, VM_PAYLOAD_SERVICE_SOCKET_NAME, VM_APK_CONTENTS_PATH}; |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 22 | use anyhow::{ensure, bail, Context, Result}; |
Alice Wang | 43c884b | 2022-10-24 09:42:40 +0000 | [diff] [blame] | 23 | use binder::{Strong, unstable_api::{AIBinder, new_spibinder}}; |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 24 | use lazy_static::lazy_static; |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 25 | use log::{error, info, Level}; |
David Brazdil | a2125dd | 2022-12-14 16:37:44 +0000 | [diff] [blame] | 26 | use rpcbinder::{RpcSession, RpcServer}; |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 27 | use std::convert::Infallible; |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 28 | use std::ffi::CString; |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 29 | use std::fmt::Debug; |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 30 | use std::os::raw::{c_char, c_void}; |
Shikha Panwar | ddc124b | 2022-11-28 19:17:54 +0000 | [diff] [blame] | 31 | use std::path::Path; |
Alan Stokes | 78d2470 | 2022-11-21 15:28:31 +0000 | [diff] [blame] | 32 | use std::ptr; |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 33 | use std::sync::{Mutex, atomic::{AtomicBool, Ordering}}; |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 34 | |
| 35 | lazy_static! { |
| 36 | static ref VM_APK_CONTENTS_PATH_C: CString = |
| 37 | CString::new(VM_APK_CONTENTS_PATH).expect("CString::new failed"); |
Alan Stokes | 0cbfdf9 | 2022-11-21 17:17:53 +0000 | [diff] [blame] | 38 | static ref PAYLOAD_CONNECTION: Mutex<Option<Strong<dyn IVmPayloadService>>> = Mutex::default(); |
Shikha Panwar | ddc124b | 2022-11-28 19:17:54 +0000 | [diff] [blame] | 39 | static ref VM_ENCRYPTED_STORAGE_PATH_C: CString = |
| 40 | CString::new(ENCRYPTEDSTORE_MOUNTPOINT).expect("CString::new failed"); |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 41 | } |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 42 | |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 43 | static ALREADY_NOTIFIED: AtomicBool = AtomicBool::new(false); |
| 44 | |
Alan Stokes | 0cbfdf9 | 2022-11-21 17:17:53 +0000 | [diff] [blame] | 45 | /// Return a connection to the payload service in Microdroid Manager. Uses the existing connection |
| 46 | /// if there is one, otherwise attempts to create a new one. |
| 47 | fn get_vm_payload_service() -> Result<Strong<dyn IVmPayloadService>> { |
| 48 | let mut connection = PAYLOAD_CONNECTION.lock().unwrap(); |
| 49 | if let Some(strong) = &*connection { |
| 50 | Ok(strong.clone()) |
| 51 | } else { |
David Brazdil | a2125dd | 2022-12-14 16:37:44 +0000 | [diff] [blame] | 52 | let new_connection: Strong<dyn IVmPayloadService> = RpcSession::new() |
| 53 | .setup_unix_domain_client(VM_PAYLOAD_SERVICE_SOCKET_NAME) |
| 54 | .context(format!("Failed to connect to service: {}", VM_PAYLOAD_SERVICE_SOCKET_NAME))?; |
Alan Stokes | 0cbfdf9 | 2022-11-21 17:17:53 +0000 | [diff] [blame] | 55 | *connection = Some(new_connection.clone()); |
| 56 | Ok(new_connection) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// 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] | 61 | fn initialize_logging() { |
| 62 | android_logger::init_once( |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 63 | 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] | 64 | ); |
| 65 | } |
| 66 | |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 67 | /// In many cases clients can't do anything useful if API calls fail, and the failure |
| 68 | /// generally indicates that the VM is exiting or otherwise doomed. So rather than |
| 69 | /// returning a non-actionable error indication we just log the problem and abort |
| 70 | /// the process. |
| 71 | fn unwrap_or_abort<T, E: Debug>(result: Result<T, E>) -> T { |
| 72 | result.unwrap_or_else(|e| { |
| 73 | let msg = format!("{:?}", e); |
| 74 | error!("{msg}"); |
| 75 | panic!("{msg}") |
| 76 | }) |
| 77 | } |
| 78 | |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 79 | /// Notifies the host that the payload is ready. |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 80 | /// Panics on failure. |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 81 | #[no_mangle] |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 82 | pub extern "C" fn AVmPayload_notifyPayloadReady() { |
Alan Stokes | f30982b | 2022-11-18 11:50:32 +0000 | [diff] [blame] | 83 | initialize_logging(); |
| 84 | |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 85 | if !ALREADY_NOTIFIED.swap(true, Ordering::Relaxed) { |
| 86 | unwrap_or_abort(try_notify_payload_ready()); |
| 87 | |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 88 | info!("Notified host payload ready successfully"); |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | /// Notifies the host that the payload is ready. |
| 93 | /// Returns a `Result` containing error information if failed. |
| 94 | fn try_notify_payload_ready() -> Result<()> { |
Alice Wang | 59a9e56 | 2022-10-04 15:24:10 +0000 | [diff] [blame] | 95 | get_vm_payload_service()?.notifyPayloadReady().context("Cannot notify payload ready") |
Alice Wang | fb46ee1 | 2022-09-30 13:08:52 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 98 | /// Runs a binder RPC server, serving the supplied binder service implementation on the given vsock |
| 99 | /// port. |
| 100 | /// |
| 101 | /// If and when the server is ready for connections (it is listening on the port), `on_ready` is |
| 102 | /// called to allow appropriate action to be taken - e.g. to notify clients that they may now |
| 103 | /// attempt to connect. |
| 104 | /// |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 105 | /// The current thread joins the binder thread pool to handle incoming messages. |
| 106 | /// This function never returns. |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 107 | /// |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 108 | /// Panics on error (including unexpected server exit). |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 109 | /// |
| 110 | /// # Safety |
| 111 | /// |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 112 | /// If present, the `on_ready` callback must be a valid function pointer, which will be called at |
| 113 | /// most once, while this function is executing, with the `param` parameter. |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 114 | #[no_mangle] |
| 115 | pub unsafe extern "C" fn AVmPayload_runVsockRpcServer( |
| 116 | service: *mut AIBinder, |
| 117 | port: u32, |
| 118 | on_ready: Option<unsafe extern "C" fn(param: *mut c_void)>, |
| 119 | param: *mut c_void, |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 120 | ) -> Infallible { |
Alan Stokes | f30982b | 2022-11-18 11:50:32 +0000 | [diff] [blame] | 121 | initialize_logging(); |
| 122 | |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 123 | // SAFETY: try_run_vsock_server has the same requirements as this function |
| 124 | unwrap_or_abort(unsafe { try_run_vsock_server(service, port, on_ready, param) }) |
| 125 | } |
| 126 | |
| 127 | /// # Safety: Same as `AVmPayload_runVsockRpcServer`. |
| 128 | unsafe fn try_run_vsock_server( |
| 129 | service: *mut AIBinder, |
| 130 | port: u32, |
| 131 | on_ready: Option<unsafe extern "C" fn(param: *mut c_void)>, |
| 132 | param: *mut c_void, |
| 133 | ) -> Result<Infallible> { |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 134 | // SAFETY: AIBinder returned has correct reference count, and the ownership can |
| 135 | // safely be taken by new_spibinder. |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 136 | let service = unsafe { new_spibinder(service) }; |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 137 | if let Some(service) = service { |
David Brazdil | 3238da4 | 2022-11-18 10:04:51 +0000 | [diff] [blame] | 138 | match RpcServer::new_vsock(service, libc::VMADDR_CID_HOST, port) { |
David Brazdil | 671e614 | 2022-11-16 11:47:27 +0000 | [diff] [blame] | 139 | Ok(server) => { |
| 140 | if let Some(on_ready) = on_ready { |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 141 | // SAFETY: We're calling the callback with the parameter specified within the |
| 142 | // allowed lifetime. |
| 143 | unsafe { on_ready(param) }; |
David Brazdil | 671e614 | 2022-11-16 11:47:27 +0000 | [diff] [blame] | 144 | } |
| 145 | server.join(); |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 146 | bail!("RpcServer unexpectedly terminated"); |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 147 | } |
David Brazdil | 671e614 | 2022-11-16 11:47:27 +0000 | [diff] [blame] | 148 | Err(err) => { |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 149 | bail!("Failed to start RpcServer: {:?}", err); |
David Brazdil | 671e614 | 2022-11-16 11:47:27 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 152 | } else { |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 153 | bail!("Failed to convert the given service from AIBinder to SpIBinder."); |
Alice Wang | 2be64f3 | 2022-10-13 14:37:35 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 157 | /// Get a secret that is uniquely bound to this VM instance. |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 158 | /// Panics on failure. |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 159 | /// |
| 160 | /// # Safety |
| 161 | /// |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 162 | /// Behavior is undefined if any of the following conditions are violated: |
| 163 | /// |
| 164 | /// * `identifier` must be [valid] for reads of `identifier_size` bytes. |
| 165 | /// * `secret` must be [valid] for writes of `size` bytes. |
| 166 | /// |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 167 | /// [valid]: ptr#safety |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 168 | #[no_mangle] |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 169 | pub unsafe extern "C" fn AVmPayload_getVmInstanceSecret( |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 170 | identifier: *const u8, |
| 171 | identifier_size: usize, |
| 172 | secret: *mut u8, |
| 173 | size: usize, |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 174 | ) { |
Alan Stokes | f30982b | 2022-11-18 11:50:32 +0000 | [diff] [blame] | 175 | initialize_logging(); |
| 176 | |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 177 | // SAFETY: See the requirements on `identifier` above. |
| 178 | let identifier = unsafe { std::slice::from_raw_parts(identifier, identifier_size) }; |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 179 | let vm_secret = unwrap_or_abort(try_get_vm_instance_secret(identifier, size)); |
Alan Stokes | e0945ad | 2022-11-24 13:29:57 +0000 | [diff] [blame] | 180 | |
| 181 | // SAFETY: See the requirements on `secret` above; `vm_secret` is known to have length `size`, |
| 182 | // and cannot overlap `secret` because we just allocated it. |
| 183 | unsafe { |
| 184 | ptr::copy_nonoverlapping(vm_secret.as_ptr(), secret, size); |
| 185 | } |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | 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] | 189 | let vm_secret = get_vm_payload_service()? |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 190 | .getVmInstanceSecret(identifier, i32::try_from(size)?) |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 191 | .context("Cannot get VM instance secret")?; |
| 192 | ensure!( |
| 193 | vm_secret.len() == size, |
| 194 | "Returned secret has {} bytes, expected {}", |
| 195 | vm_secret.len(), |
| 196 | size |
| 197 | ); |
| 198 | Ok(vm_secret) |
Andrew Scull | 102067a | 2022-10-07 00:34:40 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 201 | /// Get the VM's attestation chain. |
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 | /// |
Alan Stokes | 88805d5 | 2022-12-16 16:07:33 +0000 | [diff] [blame] | 208 | /// * `data` must be [valid] for writes of `size` bytes, if size > 0. |
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_getDiceAttestationChain(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 chain = unwrap_or_abort(try_get_dice_attestation_chain()); |
Alan Stokes | 88805d5 | 2022-12-16 16:07:33 +0000 | [diff] [blame] | 216 | if size != 0 { |
| 217 | // SAFETY: See the requirements on `data` above. The number of bytes copied doesn't exceed |
| 218 | // the length of either buffer, and `chain` cannot overlap `data` because we just allocated |
| 219 | // it. We allow data to be null, which is never valid, but only if size == 0 which is |
| 220 | // checked above. |
| 221 | unsafe { ptr::copy_nonoverlapping(chain.as_ptr(), data, std::cmp::min(chain.len(), size)) }; |
| 222 | } |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 223 | chain.len() |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | fn try_get_dice_attestation_chain() -> Result<Vec<u8>> { |
| 227 | get_vm_payload_service()?.getDiceAttestationChain().context("Cannot get attestation chain") |
| 228 | } |
| 229 | |
| 230 | /// Get the VM's attestation CDI. |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 231 | /// Panics on failure. |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 232 | /// |
| 233 | /// # Safety |
| 234 | /// |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 235 | /// Behavior is undefined if any of the following conditions are violated: |
| 236 | /// |
Alan Stokes | 88805d5 | 2022-12-16 16:07:33 +0000 | [diff] [blame] | 237 | /// * `data` must be [valid] for writes of `size` bytes, if size > 0. |
Andrew Scull | 655e98e | 2022-10-10 22:24:58 +0000 | [diff] [blame] | 238 | /// |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 239 | /// [valid]: ptr#safety |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 240 | #[no_mangle] |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 241 | 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] | 242 | initialize_logging(); |
| 243 | |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 244 | let cdi = unwrap_or_abort(try_get_dice_attestation_cdi()); |
Alan Stokes | 88805d5 | 2022-12-16 16:07:33 +0000 | [diff] [blame] | 245 | if size != 0 { |
| 246 | // SAFETY: See the requirements on `data` above. The number of bytes copied doesn't exceed |
| 247 | // the length of either buffer, and `cdi` cannot overlap `data` because we just allocated |
| 248 | // it. We allow data to be null, which is never valid, but only if size == 0 which is |
| 249 | // checked above. |
| 250 | unsafe { ptr::copy_nonoverlapping(cdi.as_ptr(), data, std::cmp::min(cdi.len(), size)) }; |
| 251 | } |
Alan Stokes | 65bbb91 | 2022-11-23 09:39:34 +0000 | [diff] [blame] | 252 | cdi.len() |
| 253 | } |
| 254 | |
| 255 | fn try_get_dice_attestation_cdi() -> Result<Vec<u8>> { |
| 256 | get_vm_payload_service()?.getDiceAttestationCdi().context("Cannot get attestation CDI") |
Andrew Scull | d64ae7d | 2022-10-05 17:41:43 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Alice Wang | c2fec93 | 2023-02-23 16:24:02 +0000 | [diff] [blame^] | 259 | /// Requests a certificate using the provided certificate signing request (CSR). |
| 260 | /// Panics on failure. |
| 261 | /// |
| 262 | /// # Safety |
| 263 | /// |
| 264 | /// Behavior is undefined if any of the following conditions are violated: |
| 265 | /// |
| 266 | /// * `csr` must be [valid] for reads of `csr_size` bytes. |
| 267 | /// * `buffer` must be [valid] for writes of `size` bytes. `buffer` can be null if `size` is 0. |
| 268 | /// |
| 269 | /// [valid]: ptr#safety |
| 270 | #[no_mangle] |
| 271 | pub unsafe extern "C" fn AVmPayload_requestCertificate( |
| 272 | csr: *const u8, |
| 273 | csr_size: usize, |
| 274 | buffer: *mut u8, |
| 275 | size: usize, |
| 276 | ) -> usize { |
| 277 | initialize_logging(); |
| 278 | |
| 279 | // SAFETY: See the requirements on `csr` above. |
| 280 | let csr = unsafe { std::slice::from_raw_parts(csr, csr_size) }; |
| 281 | let certificate = unwrap_or_abort(try_request_certificate(csr)); |
| 282 | |
| 283 | if size != 0 || buffer.is_null() { |
| 284 | // SAFETY: See the requirements on `buffer` above. The number of bytes copied doesn't exceed |
| 285 | // the length of either buffer, and `certificate` cannot overlap `buffer` because we just |
| 286 | // allocated it. |
| 287 | unsafe { |
| 288 | ptr::copy_nonoverlapping( |
| 289 | certificate.as_ptr(), |
| 290 | buffer, |
| 291 | std::cmp::min(certificate.len(), size), |
| 292 | ); |
| 293 | } |
| 294 | } |
| 295 | certificate.len() |
| 296 | } |
| 297 | |
| 298 | fn try_request_certificate(csr: &[u8]) -> Result<Vec<u8>> { |
| 299 | let certificate = get_vm_payload_service()? |
| 300 | .requestCertificate(csr) |
| 301 | .context("Failed to request certificate")?; |
| 302 | Ok(certificate) |
| 303 | } |
| 304 | |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 305 | /// Gets the path to the APK contents. |
| 306 | #[no_mangle] |
| 307 | pub extern "C" fn AVmPayload_getApkContentsPath() -> *const c_char { |
Shikha Panwar | ddc124b | 2022-11-28 19:17:54 +0000 | [diff] [blame] | 308 | VM_APK_CONTENTS_PATH_C.as_ptr() |
Alice Wang | 6bbb6da | 2022-10-26 12:44:06 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Alan Stokes | 78d2470 | 2022-11-21 15:28:31 +0000 | [diff] [blame] | 311 | /// Gets the path to the VM's encrypted storage. |
| 312 | #[no_mangle] |
| 313 | pub extern "C" fn AVmPayload_getEncryptedStoragePath() -> *const c_char { |
Shikha Panwar | ddc124b | 2022-11-28 19:17:54 +0000 | [diff] [blame] | 314 | if Path::new(ENCRYPTEDSTORE_MOUNTPOINT).exists() { |
| 315 | VM_ENCRYPTED_STORAGE_PATH_C.as_ptr() |
| 316 | } else { |
| 317 | ptr::null() |
| 318 | } |
Alan Stokes | 78d2470 | 2022-11-21 15:28:31 +0000 | [diff] [blame] | 319 | } |