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 | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 25 | use std::path::PathBuf; |
Victor Hsieh | 8fd03f0 | 2021-08-24 17:23:01 -0700 | [diff] [blame] | 26 | use std::sync::{Arc, RwLock}; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 27 | |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 28 | use crate::compilation::{compile, CompilerOutput}; |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 29 | use crate::compos_key_service::CompOsKeyService; |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 30 | use crate::fsverity; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 31 | use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFsService::IAuthFsService; |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 32 | use compos_aidl_interface::aidl::com::android::compos::{ |
| 33 | CompOsKeyData::CompOsKeyData, |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 34 | CompilationResult::CompilationResult, |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 35 | FdAnnotation::FdAnnotation, |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 36 | ICompOsService::{BnCompOsService, ICompOsService}, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 37 | }; |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 38 | use compos_aidl_interface::binder::{ |
Alan Stokes | 3189af0 | 2021-09-30 17:51:19 +0100 | [diff] [blame^] | 39 | BinderFeatures, ExceptionCode, Interface, Result as BinderResult, Strong, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 42 | const AUTHFS_SERVICE_NAME: &str = "authfs_service"; |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 43 | const DEX2OAT_PATH: &str = "/apex/com.android.art/bin/dex2oat64"; |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 44 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 45 | /// Constructs a binder object that implements ICompOsService. |
Victor Hsieh | 9ebf7ee | 2021-09-03 16:14:14 -0700 | [diff] [blame] | 46 | pub fn new_binder() -> Result<Strong<dyn ICompOsService>> { |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 47 | let service = CompOsService { |
| 48 | dex2oat_path: PathBuf::from(DEX2OAT_PATH), |
Victor Hsieh | 9ebf7ee | 2021-09-03 16:14:14 -0700 | [diff] [blame] | 49 | key_service: CompOsKeyService::new()?, |
Victor Hsieh | 8fd03f0 | 2021-08-24 17:23:01 -0700 | [diff] [blame] | 50 | key_blob: Arc::new(RwLock::new(Vec::new())), |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 51 | }; |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 52 | Ok(BnCompOsService::new_binder(service, BinderFeatures::default())) |
Alan Stokes | 9e2c5d5 | 2021-07-21 11:29:10 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 55 | struct CompOsService { |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 56 | dex2oat_path: PathBuf, |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 57 | key_service: CompOsKeyService, |
Victor Hsieh | 8fd03f0 | 2021-08-24 17:23:01 -0700 | [diff] [blame] | 58 | key_blob: Arc<RwLock<Vec<u8>>>, |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 61 | impl CompOsService { |
| 62 | fn generate_raw_fsverity_signature( |
| 63 | &self, |
| 64 | key_blob: &[u8], |
| 65 | fsverity_digest: &fsverity::Sha256Digest, |
| 66 | ) -> Vec<u8> { |
| 67 | let formatted_digest = fsverity::to_formatted_digest(fsverity_digest); |
| 68 | self.key_service.do_sign(key_blob, &formatted_digest[..]).unwrap_or_else(|e| { |
| 69 | warn!("Failed to sign the fsverity digest, returning empty signature. Error: {}", e); |
| 70 | Vec::new() |
| 71 | }) |
| 72 | } |
| 73 | } |
| 74 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 75 | impl Interface for CompOsService {} |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 76 | |
Victor Hsieh | a64194b | 2021-08-06 17:43:36 -0700 | [diff] [blame] | 77 | impl ICompOsService for CompOsService { |
Victor Hsieh | 8fd03f0 | 2021-08-24 17:23:01 -0700 | [diff] [blame] | 78 | fn initializeSigningKey(&self, key_blob: &[u8]) -> BinderResult<()> { |
| 79 | let mut w = self.key_blob.write().unwrap(); |
| 80 | if w.is_empty() { |
| 81 | *w = Vec::from(key_blob); |
| 82 | Ok(()) |
| 83 | } else { |
| 84 | Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Cannot re-initialize the key")) |
| 85 | } |
| 86 | } |
| 87 | |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 88 | fn compile( |
| 89 | &self, |
| 90 | args: &[String], |
| 91 | fd_annotation: &FdAnnotation, |
| 92 | ) -> BinderResult<CompilationResult> { |
Victor Hsieh | 51789de | 2021-08-06 16:50:49 -0700 | [diff] [blame] | 93 | let authfs_service = get_authfs_service()?; |
Victor Hsieh | 13333e8 | 2021-09-03 15:17:32 -0700 | [diff] [blame] | 94 | let output = |
| 95 | compile(&self.dex2oat_path, args, authfs_service, fd_annotation).map_err(|e| { |
| 96 | new_binder_exception( |
| 97 | ExceptionCode::SERVICE_SPECIFIC, |
| 98 | format!("Compilation failed: {}", e), |
| 99 | ) |
| 100 | })?; |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 101 | match output { |
| 102 | CompilerOutput::Digests { oat, vdex, image } => { |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 103 | let key = &*self.key_blob.read().unwrap(); |
| 104 | if key.is_empty() { |
| 105 | Err(new_binder_exception( |
| 106 | ExceptionCode::ILLEGAL_STATE, |
| 107 | "Key is not initialized", |
| 108 | )) |
| 109 | } else { |
| 110 | let oat_signature = self.generate_raw_fsverity_signature(key, &oat); |
| 111 | let vdex_signature = self.generate_raw_fsverity_signature(key, &vdex); |
| 112 | let image_signature = self.generate_raw_fsverity_signature(key, &image); |
| 113 | Ok(CompilationResult { |
| 114 | exitCode: 0, |
| 115 | oatSignature: oat_signature, |
| 116 | vdexSignature: vdex_signature, |
| 117 | imageSignature: image_signature, |
| 118 | }) |
| 119 | } |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 120 | } |
Victor Hsieh | 9ed2718 | 2021-08-25 15:52:42 -0700 | [diff] [blame] | 121 | CompilerOutput::ExitCode(exit_code) => { |
| 122 | Ok(CompilationResult { exitCode: exit_code, ..Default::default() }) |
| 123 | } |
Victor Hsieh | 6e34038 | 2021-08-13 12:18:02 -0700 | [diff] [blame] | 124 | } |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 125 | } |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 126 | |
| 127 | fn generateSigningKey(&self) -> BinderResult<CompOsKeyData> { |
| 128 | self.key_service |
| 129 | .do_generate() |
| 130 | .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string())) |
| 131 | } |
| 132 | |
| 133 | fn verifySigningKey(&self, key_blob: &[u8], public_key: &[u8]) -> BinderResult<bool> { |
| 134 | Ok(if let Err(e) = self.key_service.do_verify(key_blob, public_key) { |
| 135 | warn!("Signing key verification failed: {}", e.to_string()); |
| 136 | false |
| 137 | } else { |
| 138 | true |
| 139 | }) |
| 140 | } |
| 141 | |
Victor Hsieh | 8fd03f0 | 2021-08-24 17:23:01 -0700 | [diff] [blame] | 142 | fn sign(&self, data: &[u8]) -> BinderResult<Vec<u8>> { |
| 143 | let key = &*self.key_blob.read().unwrap(); |
| 144 | if key.is_empty() { |
| 145 | Err(new_binder_exception(ExceptionCode::ILLEGAL_STATE, "Key is not initialized")) |
| 146 | } else { |
| 147 | self.key_service |
| 148 | .do_sign(key, data) |
| 149 | .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string())) |
| 150 | } |
Victor Hsieh | 23f7359 | 2021-08-06 18:08:24 -0700 | [diff] [blame] | 151 | } |
Victor Hsieh | 272aa24 | 2021-02-01 14:19:20 -0800 | [diff] [blame] | 152 | } |
Victor Hsieh | ebb1d90 | 2021-08-06 13:00:18 -0700 | [diff] [blame] | 153 | |
| 154 | fn get_authfs_service() -> BinderResult<Strong<dyn IAuthFsService>> { |
| 155 | Ok(authfs_aidl_interface::binder::get_interface(AUTHFS_SERVICE_NAME)?) |
| 156 | } |