Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 17 | //! compsvc is a service to run compilation tasks in a PVM upon request. It is able to set up |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 18 | //! file descriptors backed by authfs (via authfs_service) and pass the file descriptors to the |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 19 | //! actual compiler. |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 20 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 21 | use anyhow::Result; |
Alan Stokes | 3189af0 | 2021-09-30 17:51:19 +0100 | [diff] [blame] | 22 | use binder_common::new_binder_exception; |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 23 | use log::warn; |
| 24 | use std::default::Default; |
Victor Hsieh | 18775b1 | 2021-10-12 17:42:48 -0700 | [diff] [blame] | 25 | use std::env; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 26 | use std::path::PathBuf; |
Victor Hsieh | 8fd03f0 | 2021-08-24 17:23:01 -0700 | [diff] [blame] | 27 | use std::sync::{Arc, RwLock}; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 28 | |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame^] | 29 | use crate::compilation::{compile_cmd, odrefresh, CompilerOutput}; |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 30 | use crate::compos_key_service::CompOsKeyService; |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 31 | use crate::fsverity; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 32 | use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::IAuthFsService; |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 33 | use compos_aidl_interface::aidl::com::android::compos::{ |
| 34 | CompOsKeyData::CompOsKeyData, |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 35 | CompilationResult::CompilationResult, |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 36 | FdAnnotation::FdAnnotation, |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 37 | ICompOsService::{BnCompOsService, ICompOsService}, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 38 | }; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 39 | use compos_aidl_interface::binder::{ |
Alan Stokes | 3189af0 | 2021-09-30 17:51:19 +0100 | [diff] [blame] | 40 | BinderFeatures, ExceptionCode, Interface, Result as BinderResult, Strong, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 43 | const AUTHFS_SERVICE_NAME: &str = "authfs_service"; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 44 | const DEX2OAT_PATH: &str = "/apex/com.android.art/bin/dex2oat64"; |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame^] | 45 | const ODREFRESH_PATH: &str = "/apex/com.android.art/bin/odrefresh"; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 46 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 47 | /// Constructs a binder object that implements ICompOsService. |
Victor Hsieh | 9ebf7ee | 2021-09-03 16:14:14 -0700 | [diff] [blame] | 48 | pub fn new_binder() -> Result<Strong<dyn ICompOsService>> { |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 49 | let service = CompOsService { |
| 50 | dex2oat_path: PathBuf::from(DEX2OAT_PATH), |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame^] | 51 | odrefresh_path: PathBuf::from(ODREFRESH_PATH), |
Victor Hsieh | 9ebf7ee | 2021-09-03 16:14:14 -0700 | [diff] [blame] | 52 | key_service: CompOsKeyService::new()?, |
Victor Hsieh | 8fd03f0 | 2021-08-24 17:23:01 -0700 | [diff] [blame] | 53 | key_blob: Arc::new(RwLock::new(Vec::new())), |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 54 | }; |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 55 | Ok(BnCompOsService::new_binder(service, BinderFeatures::default())) |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 58 | struct CompOsService { |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 59 | dex2oat_path: PathBuf, |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame^] | 60 | odrefresh_path: PathBuf, |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 61 | key_service: CompOsKeyService, |
Victor Hsieh | 8fd03f0 | 2021-08-24 17:23:01 -0700 | [diff] [blame] | 62 | key_blob: Arc<RwLock<Vec<u8>>>, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 65 | impl CompOsService { |
| 66 | fn generate_raw_fsverity_signature( |
| 67 | &self, |
| 68 | key_blob: &[u8], |
| 69 | fsverity_digest: &fsverity::Sha256Digest, |
| 70 | ) -> Vec<u8> { |
| 71 | let formatted_digest = fsverity::to_formatted_digest(fsverity_digest); |
| 72 | self.key_service.do_sign(key_blob, &formatted_digest[..]).unwrap_or_else(|e| { |
| 73 | warn!("Failed to sign the fsverity digest, returning empty signature. Error: {}", e); |
| 74 | Vec::new() |
| 75 | }) |
| 76 | } |
| 77 | } |
| 78 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 79 | impl Interface for CompOsService {} |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 80 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 81 | impl ICompOsService for CompOsService { |
Victor Hsieh | 8fd03f0 | 2021-08-24 17:23:01 -0700 | [diff] [blame] | 82 | fn initializeSigningKey(&self, key_blob: &[u8]) -> BinderResult<()> { |
| 83 | let mut w = self.key_blob.write().unwrap(); |
| 84 | if w.is_empty() { |
| 85 | *w = Vec::from(key_blob); |
| 86 | Ok(()) |
| 87 | } else { |
| 88 | Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Cannot re-initialize the key")) |
| 89 | } |
| 90 | } |
| 91 | |
Victor Hsieh | 18775b1 | 2021-10-12 17:42:48 -0700 | [diff] [blame] | 92 | fn initializeClasspaths( |
| 93 | &self, |
| 94 | boot_classpath: &str, |
| 95 | dex2oat_boot_classpath: &str, |
Victor Hsieh | 64290a5 | 2021-11-17 13:34:46 -0800 | [diff] [blame] | 96 | system_server_classpath: &str, |
Victor Hsieh | 18775b1 | 2021-10-12 17:42:48 -0700 | [diff] [blame] | 97 | ) -> BinderResult<()> { |
| 98 | // TODO(198211396): Implement correctly. |
| 99 | env::set_var("BOOTCLASSPATH", boot_classpath); |
| 100 | env::set_var("DEX2OATBOOTCLASSPATH", dex2oat_boot_classpath); |
Victor Hsieh | 64290a5 | 2021-11-17 13:34:46 -0800 | [diff] [blame] | 101 | env::set_var("SYSTEMSERVERCLASSPATH", system_server_classpath); |
Victor Hsieh | 18775b1 | 2021-10-12 17:42:48 -0700 | [diff] [blame] | 102 | Ok(()) |
| 103 | } |
| 104 | |
Victor Hsieh | f996869 | 2021-11-18 11:34:39 -0800 | [diff] [blame^] | 105 | fn odrefresh( |
| 106 | &self, |
| 107 | system_dir_fd: i32, |
| 108 | output_dir_fd: i32, |
| 109 | zygote_arch: &str, |
| 110 | ) -> BinderResult<CompilationResult> { |
| 111 | if system_dir_fd < 0 || output_dir_fd < 0 { |
| 112 | return Err(new_binder_exception( |
| 113 | ExceptionCode::ILLEGAL_ARGUMENT, |
| 114 | "The remote FDs are expected to be non-negative", |
| 115 | )); |
| 116 | } |
| 117 | if zygote_arch != "zygote64" && zygote_arch != "zygote64_32" { |
| 118 | return Err(new_binder_exception( |
| 119 | ExceptionCode::ILLEGAL_ARGUMENT, |
| 120 | "Invalid zygote arch", |
| 121 | )); |
| 122 | } |
| 123 | |
| 124 | let authfs_service = get_authfs_service()?; |
| 125 | let output = odrefresh( |
| 126 | &self.odrefresh_path, |
| 127 | system_dir_fd, |
| 128 | output_dir_fd, |
| 129 | zygote_arch, |
| 130 | authfs_service, |
| 131 | ) |
| 132 | .map_err(|e| { |
| 133 | warn!("odrefresh failed: {}", e); |
| 134 | new_binder_exception( |
| 135 | ExceptionCode::SERVICE_SPECIFIC, |
| 136 | format!("odrefresh failed: {}", e), |
| 137 | ) |
| 138 | })?; |
| 139 | match output { |
| 140 | CompilerOutput::ExitCode(exit_code) => { |
| 141 | Ok(CompilationResult { exitCode: exit_code, ..Default::default() }) |
| 142 | } |
| 143 | _ => Err(new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, "odrefresh failed")), |
| 144 | } |
| 145 | } |
| 146 | |
Victor Hsieh | 3c044c4 | 2021-10-01 17:17:10 -0700 | [diff] [blame] | 147 | fn compile_cmd( |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 148 | &self, |
| 149 | args: &[String], |
| 150 | fd_annotation: &FdAnnotation, |
| 151 | ) -> BinderResult<CompilationResult> { |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 152 | let authfs_service = get_authfs_service()?; |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 153 | let output = |
Victor Hsieh | 3c044c4 | 2021-10-01 17:17:10 -0700 | [diff] [blame] | 154 | compile_cmd(&self.dex2oat_path, args, authfs_service, fd_annotation).map_err(|e| { |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 155 | new_binder_exception( |
| 156 | ExceptionCode::SERVICE_SPECIFIC, |
| 157 | format!("Compilation failed: {}", e), |
| 158 | ) |
| 159 | })?; |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 160 | match output { |
| 161 | CompilerOutput::Digests { oat, vdex, image } => { |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 162 | let key = &*self.key_blob.read().unwrap(); |
| 163 | if key.is_empty() { |
| 164 | Err(new_binder_exception( |
| 165 | ExceptionCode::ILLEGAL_STATE, |
| 166 | "Key is not initialized", |
| 167 | )) |
| 168 | } else { |
| 169 | let oat_signature = self.generate_raw_fsverity_signature(key, &oat); |
| 170 | let vdex_signature = self.generate_raw_fsverity_signature(key, &vdex); |
| 171 | let image_signature = self.generate_raw_fsverity_signature(key, &image); |
| 172 | Ok(CompilationResult { |
| 173 | exitCode: 0, |
| 174 | oatSignature: oat_signature, |
| 175 | vdexSignature: vdex_signature, |
| 176 | imageSignature: image_signature, |
| 177 | }) |
| 178 | } |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 179 | } |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 180 | CompilerOutput::ExitCode(exit_code) => { |
| 181 | Ok(CompilationResult { exitCode: exit_code, ..Default::default() }) |
| 182 | } |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 183 | } |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 184 | } |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 185 | |
Victor Hsieh | 3c044c4 | 2021-10-01 17:17:10 -0700 | [diff] [blame] | 186 | fn compile(&self, _marshaled: &[u8], _fd_annotation: &FdAnnotation) -> BinderResult<i8> { |
| 187 | Err(new_binder_exception(ExceptionCode::UNSUPPORTED_OPERATION, "Not yet implemented")) |
| 188 | } |
| 189 | |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 190 | fn generateSigningKey(&self) -> BinderResult<CompOsKeyData> { |
| 191 | self.key_service |
| 192 | .do_generate() |
| 193 | .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string())) |
| 194 | } |
| 195 | |
| 196 | fn verifySigningKey(&self, key_blob: &[u8], public_key: &[u8]) -> BinderResult<bool> { |
| 197 | Ok(if let Err(e) = self.key_service.do_verify(key_blob, public_key) { |
| 198 | warn!("Signing key verification failed: {}", e.to_string()); |
| 199 | false |
| 200 | } else { |
| 201 | true |
| 202 | }) |
| 203 | } |
| 204 | |
Victor Hsieh | 8fd03f0 | 2021-08-24 17:23:01 -0700 | [diff] [blame] | 205 | fn sign(&self, data: &[u8]) -> BinderResult<Vec<u8>> { |
| 206 | let key = &*self.key_blob.read().unwrap(); |
| 207 | if key.is_empty() { |
| 208 | Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Key is not initialized")) |
| 209 | } else { |
| 210 | self.key_service |
| 211 | .do_sign(key, data) |
| 212 | .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string())) |
| 213 | } |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 214 | } |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 215 | } |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 216 | |
| 217 | fn get_authfs_service() -> BinderResult<Strong<dyn IAuthFsService>> { |
| 218 | Ok(authfs_aidl_interface::binder::get_interface(AUTHFS_SERVICE_NAME)?) |
| 219 | } |